我编写了一个方法,允许您向数据库添加“单词”。一旦我调用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()中的第二个参数,所以我可能知道这个:/
答案 0 :(得分:0)
尝试这种方式:
private void populateDatabase()
{
try {
long rowId = 0;
rowId = dbControl.addWord("testSyn",null,null,1);
}
catch (SQLiteException e){
e.printStackTrace();
}