0

I get NullPointerException whenever I try to insert new records to db I am not sure if the problem occurs in creation or opening here is my SQLiteHelper class:

public MySQLiteHelper(Context context){
    super(context, DB_NAME, null, DATABASE_VERSION);
 }
private static final String DB_CREATE_QUERY   = "CREATE TABLE "+TAB_NAME+" (" +
        ""+COL_ID+" integer PRIMARY KEY autoincrement," +
        ""+COL_xxxLE+" LONG," +
        ""+COL_UxxxILE+" LONG," +
        ""+COL_xxxFI+" LONG," +
        ""+COL_xxxxIFI+" LONG,"+
        ""+COL_DATE+" DATETIME  );";

private static final String DB_CREATE_QUERY_TEMP   = "CREATE TABLE "+TAB_NAME_TEMP+" (" +
        ""+COL_ID+" integer PRIMARY KEY autoincrement," +
        ""+COLxxILE+" LONG," +
        ""+COL_xxILE+" LONG," +
        ""+COL_xxFI+" LONG," +
        ""+COLxxxWIFI+" LONG,"+
        ""+COLxxxDATE+" DATETIME ) ;";

@Override
public void onCreate(SQLiteDatabase database){
    try {
        database.execSQL(DB_CREATE_QUERY);
        database.execSQL(DB_CREATE_QUERY_TEMP);
    } catch (SQLiteException exc)
    {
        Log.e("SQL",exc.toString());
    }
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    Log.w("TAHA->","Upgrading database from version: "+oldVersion+" to "+newVersion+". This will destroy all existing data");
    db.execSQL("DROP TABLE IF EXISTS "+ TAB_NAME);
    db.execSQL("DROP TABLE IF EXISTS "+ TAB_NAME_TEMP);
    onCreate(db);
}

this is my RecordSource.java where I insert records

public class RecordSource {

private MySQLiteHelper dbHelper;
private SQLiteDatabase database;

private String[] myColumns =
        {
                MySQLiteHelper.xxxLE,
                MySQLiteHelper.xxxxILE,
                MySQLiteHelper.xxxxFI,
                MySQLiteHelper.xxxxIFI,
};
/**
 *CONSTRUCTOR
 * calling MySQLiteHelper class which maintains my DB each time
 * to make sure my DB is ready
 */
public RecordSource(Context context)
{
    dbHelper = new MySQLiteHelper(context);
}



/**
 * Open and close methods, used to free memory
 * @throws SQLiteException
 */
public void open() throws SQLiteException
{
    database = dbHelper.getWritableDatabase();

}

public void close()
{
    dbHelper.close();
}


public boolean createRecord(boolean firstLaunch)
{
    if(firstLaunch)
        freshStart();
    else {
        Record rec = new Record( getLastRecord("temp_record"), true);
        insertRecord(rec, MySQLiteHelper.TAB_NAME);
        Log.w("taha->", "another record inserted");
    }
    return true;
}

public void freshStart()
{
    Record firstRecord = new Record();
    insertRecord(firstRecord, MySQLiteHelper.TAB_NAME_TEMP);
    Record rec = new Record( getLastRecord("temp_record"), true);
    insertRecord(rec,MySQLiteHelper.TAB_NAME);
    Log.w("taha->", "fresh record inserted");
}
public boolean insertRecord(Record rec,String tab)
{
    ContentValues rec_values = new ContentValues();
    rec_values.put(myColumns[0],rec.getDown_mobile());
    rec_values.put(myColumns[1],rec.getUp_mobile());
    rec_values.put(myColumns[2],rec.getDown_wifi());
    rec_values.put(myColumns[3],rec.getUp_wifi());

    try {
        database.insert(tab, null, rec_values);
    } catch (SQLiteException ex)
    {
        Log.w("taha->", ex.toString());
        return false;
    }
    return true;
}

and I call it from a MyService

 Context ctx = getApplicationContext();

                    rec = new RecordSource(context);

this is the error

  Process: com.xx.xx.nxxr, PID: 16742
java.lang.NullPointerException
        at com.xxxr.RecordSource.insertRecord(RecordSource.java:105)
        at com.xxer.RecordSource.freshStart(RecordSource.java:83)
        at comxxxher.RecordSource.createRecord(RecordSource.java:71)
        at com.xxxer.MyService$1.run(MyService.java:43)

I checked in the db file and the database exists but database variable is always null

3
  • can you please give us the implementation of the insertRecord() method? Commented Dec 28, 2014 at 19:32
  • Sorry for that, I edited my post and added missing methods Commented Dec 28, 2014 at 19:44
  • why in your service have this? rec = new RecordSource(context); and the name of your context is ctx? Commented Dec 28, 2014 at 20:13

1 Answer 1

1

change your createRecord() method to this:

public boolean createRecord(boolean firstLaunch)
{
    open();
    if(firstLaunch)
        freshStart();
    else {
        Record rec = new Record( getLastRecord("temp_record"), true);
        insertRecord(rec, MySQLiteHelper.TAB_NAME);
        Log.w("taha->", "another record inserted");
    }
    close();
return true;
}

for your info you have to open and close the database before and after any query

Sign up to request clarification or add additional context in comments.

2 Comments

new error in database = dbHelper.getWritableDatabase();
ok i think i found it. in RecordSource class change your private MySQLiteHelper dbHelper; to this: private SQLiteOpenHelper dbHelper;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.