mercredi 17 juin 2015

Subclass of SQLiteDatabaseHelper throws NullpointerException when calls getReadebleDatabase()

I have a subclass of SQLiteDatabaseOpenHelper called DatabaseSuper which overrides onCreate and onUpdate methods. DatabaseController class is the subclass of DatabaseSuper which intended to implement all the application specific database actions.

My problem is, when I am calling getReadableDatabase() in a method in DatabaseController class, it thorws a Nullpointer exception. But if I am pass a SQLiteDatabase object as a parameter to that method which taken from the base class (DatabaseSuper), no exception is throw.

Method

public String sendDate(SQLiteDatabase db) {

    //SQLiteDatabase db = getReadableDatabase();                 *line 1*

    Cursor cursor = db.rawQuery("select date from todo", null);
    StringBuffer stringBuffer = new StringBuffer();
    while (cursor.moveToNext()) {
        String date = cursor.getString(0);  
        stringBuffer.append(date + ":");
    }
    cursor.close();
    return stringBuffer.toString();
}

Calling method from activity class

String data = dataControllerObject.sendDate(new DatabaseSuper().getReadableDatabase());

Error

06-18 04:28:50.239: E/AndroidRuntime(5147): 
FATAL EXCEPTION: main
06-18 04:28:50.239: 
E/AndroidRuntime(5147): 
java.lang.RuntimeException:Unable to start activity ComponentInfo{com.example.dolist/com.example.dolist.SearchListView}:
java.lang.NullPointerException
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-18 04:28:50.239:

E/AndroidRuntime(5147):     
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.os.Looper.loop(Looper.java:137)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.ActivityThread.main(ActivityThread.java:5041)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at java.lang.reflect.Method.invokeNative(Native Method)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at java.lang.reflect.Method.invoke(Method.java:511)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at dalvik.system.NativeStart.main(Native Method)
06-18 04:28:50.239: E/AndroidRuntime(5147):
Caused by: java.lang.NullPointerException
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at com.example.dolist.DataController.sendDate(DataController.java:37)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at com.example.dolist.SearchListView.onCreate(SearchListView.java:34)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.Activity.performCreate(Activity.java:5104)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-18 04:28:50.239: E/AndroidRuntime(5147):
... 11 more

Aucun commentaire:

Enregistrer un commentaire