-3

so i have this app i'm trying to make that requires updating the database it's using, but it keeps giving me this error:

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

here is the function that updates my database:

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);
    }

i've tried using ContentValue and it didn't work so i thought i'd use rawQuery, maybe i'll figure out what i'm doing wrong, plus i can't seem to be able to find tutorials the thoroughly explain how to use ContentValue, this is my first app and i'm learning how to do it while doing it, any help would be great!

EDIT: okay upon thinking i changed up 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);

and started getting a different error:

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));

}
lua
  • 41
  • 1
  • 8
  • where is `mDataBase` set? – Suraj Rao Apr 15 '17 at 07:33
  • 7
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Suraj Rao Apr 15 '17 at 07:34
  • it's a bit confusing, i'm trying to update a specific field in col3, aka where the _id is 2, but it seems that the query doesn't do that? i'm sorry for the stupid question ): @MikeM. – lua Apr 15 '17 at 07:56
  • 1
    oh my god, thank you! it stopped crashing, although it's not updating the database the way it's supposed to, but thank you! @MikeM. – lua Apr 15 '17 at 08:07

1 Answers1

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));
Akshay More
  • 416
  • 4
  • 9
  • okay, i think it's working, but i have one more question, the data i'm updating stays there, right? i mean, when i close the app it doesn't reset, right? @AkshayMore – lua Apr 15 '17 at 11:38