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");
}
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
Aucun commentaire:
Enregistrer un commentaire