我试图将类别,标题,正文的字符串保存到数据库中,但是每当我尝试执行saveState()
方法时,我都会收到NullPointerException。有谁可以帮助我?
//exectued when I press Save button
public void onClickSave(View v) {
saveState();
}
// fire up creation
private void saveState() {
String title = mJokeTitle.getText().toString();
String body = mJokeBody.getText().toString();
long id = mDbHelper.createJoke(enteredCat, title, body);
}
我的数据库架构
public class NotesDbAdapter {
public static final String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_CATEGORY = "category";
public static final String KEY_ROW_ID = "_id";
private static final String TAG = "NotesDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "my_database";
private static final String DATABASE_TABLE = "jokes";
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE + " (_id integer primary key autoincrement, "
+ "title text not null, body text not null, category text not null unique);";
public long createJoke(String category, String title, String body) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_CATEGORY, category);
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_BODY, body);
// return rowId if successful, otherwise -1
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
答案 0 :(得分:0)
private void saveState() {
String title ="";
String body="";
if(mJokeTitle.getText()!=nll){
title = mJokeTitle.getText().toString();
}else{
System.out.println("mJokeTitle.getText() value is null you need to look into why this value is Null");
}
if( mJokeBody.getText()!=nll){
body = mJokeBody.getText().toString();
}else{
System.out.println(" mJokeBody.getText() value is null you need to look into why this value is Null");
}
if(mDbHelper!=null){
long id = mDbHelper.createJoke(enteredCat, title, body);
}else{
System.out.println("mDbHelper value is null you need to look into why this value is Null");
}
}
答案 1 :(得分:0)
您应该提供更多代码,在saveState()方法中提供变量'mDbHelper'的代码未初始化。如果它是第二个变量'enteredCat',我们无法知道它是否已初始化。
答案 2 :(得分:0)
mDbHelper是空的