Android:空指针异常

时间:2011-12-07 02:56:08

标签: android sqlite nullpointerexception

我继续在NUllPointerException上线:

return mDb.insert(DATABASE_TABLE, null, initialValues);

我不知道为什么。谁能帮我?这是我的应用程序的SQLite数据库:

public MessagesDBAdapter open() throws SQLException {
            mDbHelper = new DatabaseHelper(mCtx);
            mDb = mDbHelper.getWritableDatabase();
            return this;
        }

    public void close() {
        mDbHelper.close();
    }

    public long createNote(String phoneNo, String message) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_RECIPIENT, phoneNo);
        initialValues.put(KEY_MESSAGE, message);

        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

    public boolean deleteNote(long rowId) {

        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

1 个答案:

答案 0 :(得分:1)

似乎mDb没有设置好。你在调用createNote之前调用了open()吗?否则,尝试在createNote方法中添加对open()的调用,如下所示:

public long createNote(String phoneNo, String message) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_RECIPIENT, phoneNo);
    initialValues.put(KEY_MESSAGE, message);

    open();

    return mDb.insert(DATABASE_TABLE, null, initialValues);
}