0

My SQLite Row Update method causes a NullPointer Exception when trying to update a value.

 public void addLike(DBOps dop, String productid,String newValue,String oldValue)
 {
    try
    {
        ContentValues updatedValues = new ContentValues();
        updatedValues.put(TableInfo.LIKED, newValue); 
        String where="pid = ?";
        updatedb.update(TableInfo.TABLE_NAME,updatedValues, where, 
                                                new String[]{productid});
    }
    catch(Exception e)
    {
        Log.e("UPDATE ERROR", "Couldn't update likes!");
        Log.e("UPDATE ERROR", Log.getStackTraceString(e.getCause()
                                                      .getCause()));
    }
  }

My table name is stated in: TableInfo.TABLE_NAME My column name is stated in: TableInfo.LIKED

Old value is retrieved by a different Get Method. The ProductID and the NewValue is not null. I checked and they contain the proper values. Is my Update method correct?

Pjayness
  • 365
  • 1
  • 6
  • 22
  • Did you checked that `updatedb` is not null ? – sol4me Jan 03 '15 at 13:30
  • It is globally declared as "private SQLiteDatabase updatedb;" @sol4me – Pjayness Jan 03 '15 at 13:31
  • 2
    you need to initialize it. But default its null and hence nullpointer exception. – SMA Jan 03 '15 at 13:33
  • 1
    First, use `SQLiteOpenHelper`, rather than a raw `SQLiteDatabase`, unless you have a strong reason not to. Second, obtain the `SQLiteOpenHelper` via some centralized static method, where that method can initialize the `SQLiteOpenHelper` if it has not been created yet. – CommonsWare Jan 03 '15 at 13:36

0 Answers0