更新数据库时NullPointerExcpetion

时间:2017-04-15 07:31:56

标签: android sqlite nullpointerexception sql-update android-sqlite

所以我有这个应用程序我试图制作它需要更新它使用的数据库,但它一直给我这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference

这是更新我的数据库的函数:

void insertdata(String id, int num) {
     //   ContentValues cv = new ContentValues();
     //   cv.put(Enwd, Integer.toString(num));
    //    Log.d(LOG_TAG,String.valueOf(cv));
    //    if (id == null){
     //       Log.d("ID IS","NULL");
     //   } else Log.d("ID IS","NOT NULL");
        String thing = "update "+TABLE_NAME+" set "+col3+ "=num"+" where _id ="+id;
        Log.d("CURS ",thing);
        Cursor curs = mDataBase.rawQuery(thing,null);

        curs.close();
      //  mDataBase.update(TABLE_NAME,cv,"_id= "+id,null);
    }

我尝试过使用ContentValue并且它没有工作,所以我认为我使用的是rawQuery,也许我会弄清楚我做错了什么,而且我可以&我#39;似乎能够找到教程彻底解释如何使用ContentValue,这是我的第一个应用程序,我在学习如何做到这一点,任何帮助都会很棒!

编辑:好的,我认为我改变了insertdata:

 void insertdata(String id, int num) {
        SQLiteDatabase db = getReadableDatabase();
     //   ContentValues cv = new ContentValues();
     //   cv.put(Enwd, Integer.toString(num));
    //    Log.d(LOG_TAG,String.valueOf(cv));
    //    if (id == null){
     //       Log.d("ID IS","NULL");
     //   } else Log.d("ID IS","NOT NULL");
        String thing = "update "+TABLE_NAME+" set "+col3+ "=num"+" where _id ="+id;
        Log.d("CURS ",thing);
        Cursor curs = db.rawQuery(thing,null);

        curs.close();
      //  mDataBase.update(TABLE_NAME,cv,"_id= "+id,null);

并开始收到其他错误:

android.database.sqlite.SQLiteException: no such column: num (code 1): , while compiling: update wordsdata set col3=num where _id =2
                                                                                    #################################################################
                                                                                    Error Code : 1 (SQLITE_ERROR)
                                                                                    Caused By : SQL(query) error or missing database.
                                                                                        (no such column: num (code 1): , while compiling: update wordsdata set col3=num where _id =2)

EDIT2:

void insertyes(String id, int num) {
    SQLiteDatabase db = getWritableDatabase();
    Log.d("THE NUM",Integer.toString(num));
 //   ContentValues cv = new ContentValues();
 //   cv.put(Enwd, Integer.toString(num));
//    Log.d(LOG_TAG,String.valueOf(cv));
//    if (id == null){
 //       Log.d("ID IS","NULL");
 //   } else Log.d("ID IS","NOT NULL");
   /* String thing = "update "+TABLE_NAME+" set "+Enyes+ "="+num +" where _id ="+id;
    Log.d("CURS ",thing);
    Cursor curs = db.rawQuery(thing,null);

    curs.close();*/
    ContentValues cv = new ContentValues();
    Log.d(LOG_TAG, "--- Update table: ---");

    cv.put(Enyes, num);


    int updCount = db.update(TABLE_NAME, cv, "_id = "+id,null);
    Log.d("THE UPDATED SHIT",Integer.toString(updCount));

}

1 个答案:

答案 0 :(得分:0)

SQLiteDatabase db = this.getWritableDatabase();
    final String COLUMN_NAME = "Enyes";
    final String WHERE_CLAUSE = "_id = '" + id + "'";
    ContentValues values = new ContentValues();
    values.put(COLUNM_NAME, num);
    db.update(TABLE_NAME, values, WHERE_CLAUSE, null);

    Cusror cursor = db.rawQuery("SELECT " + COLUMN_NAME + " FROM " + TABLE_NAME + "WHERE _id ='" + id "'");

    cursor.moveToFirst();
    Log.e(COLUMN_NAME, cursor.getString(0));