3

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)

            

4
  • Looks like your context may be empty Commented Jul 8, 2014 at 17:56
  • 1
    Post logcat exception. Commented Jul 8, 2014 at 17:58
  • And show where you are calling this method Commented Jul 8, 2014 at 18:00
  • Okay I added more information - hopefully that helps clear it up! Commented Jul 8, 2014 at 19:33

1 Answer 1

1

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().

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

3 Comments

Okay, it makes sense that the context is calling the NPE. But if I want to initialize the database helper in a service class that doesn't have onCreate(), where should I put it?
Services have onCreate() too
I moved my initialization further down in the class (in onHandleIntent(), as this is an IntentService class and I'm not supposed to call onCreate() directly). But that worked, so thank you!

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.