Search code examples
androidsqliteandroid-sqlitesqliteopenhelper

SQLiteOpenHelper getInstance() Method Throws Null Pointer Exception


I have a DatabaseHelper class as part of my app (a snippet is below):

public class DatabaseHelper extends SQLiteOpenHelper {

private static final int VERSION = 1;
private static DatabaseHelper mInstance;

// Get database instance
public static DatabaseHelper getInstance(Context c) {
    if (mInstance == null) {
        mInstance = new DatabaseHelper(c.getApplicationContext()); //fails here
    }
    return mInstance;
}

// Database Helper constructor
private DatabaseHelper(Context context) {
    super(context, DB_NAME, null, VERSION);
    //this.mContext = context;
}

I added the getInstance() method to guarantee a singleton property, as I need to access my database in various places of my app while only ever using one instance. Edit: I initialize the helper with the code below in the very beginning of a class that extends IntentService (so it's not an activity, and it doesn't have an onCreate() method).

private DatabaseHelper db = DatabaseHelper.getInstance(this);

However, this causes my app to fail with "thread exiting with uncaught exception" and then "NullPointerException." What am I doing wrong here? Am I initializing the helper correctly? Here's the logcat:

07-08 13:29:16.896  25205-25205/edu.swarthmore.cs.mdash.activitytype D/Place:﹕ in getInstance of DBHelper
07-08 13:29:16.897  25205-25205/edu.swarthmore.cs.mdash.activitytype D/AndroidRuntime﹕ Shutting down VM
07-08 13:29:16.898  25205-25205/edu.swarthmore.cs.mdash.activitytype W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418f8d40)
07-08 13:29:16.910  25205-25205/edu.swarthmore.cs.mdash.activitytype E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: edu.swarthmore.cs.mdash.activitytype, PID: 25205
java.lang.RuntimeException: Unable to instantiate service edu.swarthmore.cs.mdash.activitytype.ActivityRecognitionIntentService: java.lang.NullPointerException
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:2570)
        at android.app.ActivityThread.access$1800(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
        at edu.swarthmore.cs.mdash.activitytype.DatabaseHelper.getInstance(DatabaseHelper.java:35)
        at edu.swarthmore.cs.mdash.activitytype.ActivityRecognitionIntentService.<init>(ActivityRecognitionIntentService.java:52)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1208)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:2567)
        at android.app.ActivityThread.access$1800(ActivityThread.java:139)

            


Solution

  • You're calling the method too early when initializing member variables of some Context class such as an Activity (as indicated by private). At that point the context has not yet been set up and calling getApplicationContext() will NPE due to missing base context.

    Context is only set up just before onCreate() in the lifecycle. Move your initialization to onCreate().