1

我知道我有一个空指针,但我不知道它发生在哪里或为什么。希望能在这里得到一些帮助。

Button add = (Button) findViewById(R.id.addfencebutton);
add.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View add) {
        //code to store in fence locations database
        createData();


    }
});
}


public void createData(){
    Course course = new Course();
    course.setcoursename("courseNamefield");
    course.setfencename("fenceNamefield");
    course.setAlocation("standardAfield");
    course.setBlocation("standardBfield");
    course = datasource.create(course);
    Log.i(course.getcoursename(), null);
    //Log.i("course created with id" + course.getcourseid(), null);
//  Log.i("course name is " + course.getcoursename(), null);
}

文件课程DBopenHelper

public class courseDBOpenHelper extends SQLiteOpenHelper {

private static final String LOGTAG = "courseDatabase";

private static final String DATABASE_NAME = "courses.db";
private static final int DATABASE_VERSION = 1;

public static final String TABLE_COURSES = "courses";
public static final String COLUMN_ID = "coursesID";
public static final String COLUMN_COURSENAME = "coursename";
public static final String COLUMN_FENCENAME = "fencename";
public static final String COLUMN_ALOCATION = "alocation";
public static final String COLUMN_BLOCATION = "blocation";

private static final String TABLE_CREATE = 
        "CREATE TABLE " + TABLE_COURSES + " (" +
        COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        COLUMN_COURSENAME + " TEXT, " +
        COLUMN_FENCENAME + " TEXT, " +
        COLUMN_ALOCATION + " NUMERIC, " +
        COLUMN_BLOCATION +  " NUMBERIC " +
        ")";

public courseDBOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    Log.i(LOGTAG, "table created");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_COURSES);
    onCreate(db);

}

}

courseDataSource.java

public class courseDataSource {

public static final String LOGTAG = "courseDatabase";

SQLiteOpenHelper dbhelper;
SQLiteDatabase database;

public courseDataSource(Context context) {
    dbhelper = new courseDBOpenHelper(context);
    database = dbhelper.getWritableDatabase();
}

public void open(){
    Log.i(LOGTAG, "database open");
    database = dbhelper.getWritableDatabase();
}

public void close(){
    Log.i(LOGTAG, "database closed");
    dbhelper.close();
}

public Course create(Course course) {
    ContentValues values = new ContentValues();
    values.put(courseDBOpenHelper.COLUMN_COURSENAME, course.getcourseid());
    values.put(courseDBOpenHelper.COLUMN_FENCENAME, course.getcoursename());
    values.put(courseDBOpenHelper.COLUMN_ALOCATION, course.getAlocation());
    values.put(courseDBOpenHelper.COLUMN_BLOCATION, course.getBlocation());
    long insertid = database.insert(courseDBOpenHelper.TABLE_COURSES, null, values);
    course.setcourseid(insertid);
    return course;
}

}

课程.java

public class Course {
private long courseid;
private String coursename;
private String fencename;
private String Alocation;
private String Blocation;


public long getcourseid(){
    return courseid;
}

public void setcourseid(long courseid) {
    this.courseid = courseid;
}

public String getcoursename(){
    return coursename;
}

public void setcoursename(String string){
    this.coursename = coursename;
}

public String getfencename(){
    return fencename;
}

public void setfencename(String fencename){
    this.fencename = fencename;
}

public String getAlocation(){
    return Alocation;
}

public void setAlocation(String view){
    this.Alocation = view;
}

public String getBlocation(){
    return Blocation;
}

public void setBlocation(String view){
    this.Blocation = view;
}


}

错误日志

02-27 11:03:42.058: E/AndroidRuntime(909): FATAL EXCEPTION: main
02-27 11:03:42.058: E/AndroidRuntime(909): java.lang.NullPointerException: println needs a message
02-27 11:03:42.058: E/AndroidRuntime(909):  at android.util.Log.println_native(Native Method)
02-27 11:03:42.058: E/AndroidRuntime(909):  at android.util.Log.i(Log.java:159)
02-27 11:03:42.058: E/AndroidRuntime(909):  at com.greygoosefarmpa.fencecoursebuilder.addFence.createData(addFence.java:155)
02-27 11:03:42.058: E/AndroidRuntime(909):  at com.greygoosefarmpa.fencecoursebuilder.addFence$4.onClick(addFence.java:140)
02-27 11:03:42.058: E/AndroidRuntime(909):  at android.view.View.performClick(View.java:4202)
02-27 11:03:42.058: E/AndroidRuntime(909):  at android.view.View$PerformClick.run(View.java:17340)
02-27 11:03:42.058: E/AndroidRuntime(909):  at android.os.Handler.handleCallback(Handler.java:725)
02-27 11:03:42.058: E/AndroidRuntime(909):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-27 11:03:42.058: E/AndroidRuntime(909):  at android.os.Looper.loop(Looper.java:137)
02-27 11:03:42.058: E/AndroidRuntime(909):  at android.app.ActivityThread.main(ActivityThread.java:5039)
02-27 11:03:42.058: E/AndroidRuntime(909):  at java.lang.reflect.Method.invokeNative(Native Method)
02-27 11:03:42.058: E/AndroidRuntime(909):  at java.lang.reflect.Method.invoke(Method.java:511)
02-27 11:03:42.058: E/AndroidRuntime(909):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-27 11:03:42.058: E/AndroidRuntime(909):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-27 11:03:42.058: E/AndroidRuntime(909):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

5

你的错误很明显

E/AndroidRuntime(909): java.lang.NullPointerException: println needs a message 02-27 11:03:42.058:

您必须为日志消息提供消息。它不能为空。

Log.i(course.getcoursename(), null);

应该

Log.i(course.getcoursename(), "Some not null string");
于 2013-02-27T16:16:53.283 回答