SQLiteOpenHelper getInstance()方法抛出空指针异常

时间:2014-07-08 17:54:28

标签: android sqlite android-sqlite sqliteopenhelper

我有一个DatabaseHelper类作为我的应用程序的一部分(下面的代码片段):

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;
}

我添加了getInstance()方法以保证单例属性,因为我需要在我的应用程序的各个位置访问我的数据库,而只使用一个实例。 编辑:我在扩展IntentService的类的最开头使用下面的代码初始化帮助器(因此它不是活动,并且它没有onCreate()方法)。

private DatabaseHelper db = DatabaseHelper.getInstance(this);

然而,这导致我的应用程序失败,“线程退出未捕获的异常”,然后是“NullPointerException”。我在这做错了什么?我正确地初始化帮助器了吗? 这是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)

1 个答案:

答案 0 :(得分:1)

在初始化某些Context类的成员变量(例如Activity(由private表示)时,您过早地调用该方法。此时尚未设置上下文,并且由于缺少基本上下文而调用getApplicationContext() NPE。

Context仅在生命周期的onCreate()之前设置。将初始化移至onCreate()