mardi 17 février 2015

android sqlite insert exception

I'm running into a NullPointerException when I'm inserting rows in onCreate() in my SQLiteOpenHelper. The exception occurs at the return statement of the insert method. Before calling insertIng(), sqliteDBInstance is not null and this.sqliteDBInstance is null. Once in insertIng(), both are null.


Here are the relevant chunks of code:



private SQLiteDatabase sqliteDBInstance = null;


public void openDB() throws SQLException{
Log.i("openDB", "Checking sqliteDBInstance...");
if(this.sqliteDBInstance == null){
Log.i("openDB", "Creating sqliteDBInstance...");
this.sqliteDBInstance = this.getWritableDatabase();
}
}

public void onCreate(SQLiteDatabase sqliteDBInstance){
Log.i("onCreate", "Creating the database...");
sqliteDBInstance.execSQL(DB_CREATE_SCRIPT);
insertIng("C","V");
}

public long insertIng(String ingName, String constructor){
ContentValues contentValues = new ContentValues();
contentValues.put(DB_COLUMN_1_NAME, ingName);
contentValues.put(DB_COLUMN_2_NAME, constructor);
Log.i(this.toString() + " - insertIng", "Inserting: " + ingName);
return this.sqliteDBInstance.insert(DB_TABLE_NAME, null, contentValues);
}


Logcat:



Caused by: java.lang.NullPointerException
at com.example.ciltkick.nom.SQLiteIngAssistant.insertIng(SQLiteIngAssistant.java:934)
at com.example.ciltkick.nom.SQLiteIngAssistant.onCreate(SQLiteIngAssistant.java:38)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.example.ciltkick.nom.SQLiteIngAssistant.openDB(SQLiteIngAssistant.java:917)
at com.example.ciltkick.nom.MainActivity.onCreate(MainActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
at android.app.ActivityThread.access$600(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4787)
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:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)

Aucun commentaire:

Enregistrer un commentaire