简体   繁体   中英

Why is getApplicationContext throwing nullPointerException, in singleton getInstance?

I have created a class (GameHelper) which extends SQLiteOpenHelper. I want it to be a singleton, so I can create it and have it store the contents of the database during runtime, with all other classes referencing it.

I followed the advice here , (Approach 1).

So I when I need to reference the database, I don't create a new instance of GameHelper, I call getInstance. Here is the relevant code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_info);
    populate(getIntent().getStringExtra("game"), getIntent().getStringExtra("game"));
}

public void populate(String gameToGo, String game) {
    DataTable game = gameHelper.getgame(game, gameToGo); //GameInfo.java:12
    TextView tv = (TextView) findViewById(R.id.tableId);
    String text = game.TABLE_ID;
    tv.setText(text);
}

Which refers to the following method in GameHelper:

private static GameHelper sInstance;

public static synchronized GameHelper getInstance(Context context) {

// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
// See this article for more information: http://bit.ly/6LRzfx
    if (sInstance == null) {
        sInstance = new GameHelper(context.getApplicationContext()); //GameHelper.java:60
    }

    return sInstance;
}

The problem occurs on the line sInstance = new GameHelper(context.getApplicationContext()); , which throws a NullPointerException, as shown here:

11-08 10:14:54.974  16214-16214/com.example.android.whichgame E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.android.whichgame, PID: 16214
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.whichgame/com.example.android.whichgame.GameInfo}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2177)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2301)
    at android.app.ActivityThread.access$800(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5212)
    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:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
 at com.example.android.whichgame.GameHelper.getInstance(GameHelper.java:60) 
    at com.example.android.whichgame.GameInfo.<init>(GameInfo.java:12)
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1208)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1062)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2155)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2301)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
                at android.os.Handler.dispatchMessage(Handler.java:106)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5212)
                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:786)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
                at dalvik.system.NativeStart.main(Native Method)

I have spent all day scouring the internet for answers, and while I have found numerous responses to similar problems, I haven't found anything that has helped. The best and most popular answer seems to be that I need to set android:name for <application> in AndroidManifest, but herein lies another problem. I'm working in Android Studio, and when I try to add my application name, the name turns red. I suppose it's possible that I'm doing it wrong! My package (as defined in the manifest) is "com.example.android.whichgame", though the app itself (and the folder it is in) is called Whichgame.

So my question is this: What am I missing that is causing getApplicationContext() to kick out the NPE when called through getInstance? And if it is because I haven't set android:name, what is causing Studio and the compiler to reject the name?

You're using your GameInfo activity as a Context too early at initialization phase. Postpone the initialization that requires a valid Context to onCreate() or later in the activity lifecycle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM