jeudi 10 septembre 2015

ContentValues passing null values, even when initialised

I am having error in this for past couple of days, have tried using breakpoints to figure why is it giving null pointer exception, but i am not able to understand which value is getting stuck. How can i make db be not null, so as to run this code. I have an abstract class that calls this method on certain conditional statement. It has to return a long value. Which is to be used for some other method.

This method long save is implemented in another class which returns a value after calling db.insert, but i am getting Null Pointer Exception

java.lang.NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues)' on a null object reference

I have following doubts :

  1. Why is this giving error when i am using it in Fragment, but when i am using with Activity it's working fine. What can be the reason.
  2. Also How is getWritableDatabase gets called when i pass db as an argument.

    long save(SQLiteDatabase db) {
    
    
            ContentValues cv = new ContentValues();
            long now = System.currentTimeMillis();
            cv.put(COL_CREATEDTIME, now);
            cv.put(COL_MODIFIEDTIME, now);
            //cv.putNull(COL_MODIFIEDTIME);
            cv.put(COL_NAME, name==null ? "" : name);
            //if (fromDate != null)
            cv.put(COL_FROMDATE, fromDate==null ? "" :fromDate);
            //if (toDate != null)
            cv.put(COL_TODATE, toDate==null ? "" :toDate);
            //if (rule != null)
            cv.put(COL_RULE, rule==null ? "" :rule);
            //if (interval != null)
            cv.put(COL_INTERVAL, interval==null ? "" :interval);
            cv.put(COL_SOUND, sound ? 1 : 0);
            //if (sound != null)
    
            //Log.e(TAG, "Error inserting " + now);
            return db.insert(TABLE_NAME,null, cv);
    
    

    }

Aucun commentaire:

Enregistrer un commentaire