0

The following sql code

SQLiteDatabase db = this.getWritableDatabase();

        if(getCartItems(cart.id).size()>0){

            ContentValues values = new ContentValues();

            values.put(COL_ITEM_ID, cart.id);
            values.put(COL_CART_ADDDATE, cart.addDate);
            long re = db.insert(TABLE_NAME_CART, null, values);
            db.close();

            return re > 0;
        }else{
            db.close();
            return false;
        }

this code returns the following error :

java.lang.NullPointerException
at android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.java:290)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:115)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
at phoenix.metro.uis.DBHelper.insertCart(DBHelper.java:120)
at phoenix.metro.uis.SearchActivity$4.onClick(SearchActivity.java:171)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

The error in the following code :

long re = db.insert(TABLE_NAME_CART, null, values);
Adham
  • 63,550
  • 98
  • 229
  • 344
  • http://stackoverflow.com/questions/7999075/sqlitedatabase-close-function-causing-nullpointerexception-when-multiple-threa – Vrashabh Irde Nov 01 '12 at 08:57
  • The error is in long re = db.insert(TABLE_NAME_CART, null, values); not in db.close() – Adham Nov 01 '12 at 09:16
  • 1
    Well basically I can think of a couple of reasons why there is npe - obviously db is null so either db has been closed or getWritableDatabase() does not return a db, debug at getwritabledatabase and see if it is returning a db – Vrashabh Irde Nov 01 '12 at 09:22

0 Answers0