[英]Android SQLiteDatabase insert() method throws Null Pointer Exception
我写了一种方法,可以让您向数据库中添加一个“单词”。 一旦我调用addWord()方法,当它尝试database.insert()时,它将引发Null指针异常。
public class DatabaseControl {
// columns for table
public static final String KEY_ROWID = "_id";
public static final String KEY_SYNID = "synsetID";
public static final String KEY_WORD = "synsetID";
public static final String KEY_DESCRIPTION = "description";
public static final String KEY_POPULARITY = "popularity";
// table name
public static final String DATABASE_TABLE = "wordnet"; // Name of the Database
private Context context;
private SQLiteDatabase database;
private DatabaseHelper dbHelper;
public DatabaseControl(Context context){
this.context = context;
}
public DatabaseControl open() throws SQLiteException {
dbHelper = new DatabaseHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
public void close(){
dbHelper.close();
}
public long addWord (String synId, String word, String description, int popularity) {
ContentValues setUpVals = createContentValues(synId, word, description, popularity);
// SEEMS TO THROW NULL POINTER EXCEPTION
return database.insert(DATABASE_TABLE, null , setUpVals);
}
我在这里调用此方法:
private void populateDatabase(){
try {
long rowId = 0;
rowId = dbControl.addWord("testSyn","","",1); // Add a row to the database for test
}
catch (SQLiteException e){
e.printStackTrace();
}
真的不明白为什么会引发此错误,而且我也不完全了解insert()中的第二个参数,所以就我所知可能是:/
尝试这种方式:
private void populateDatabase()
{
try {
long rowId = 0;
rowId = dbControl.addWord("testSyn",null,null,1);
}
catch (SQLiteException e){
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.