-1

Beginner Java/Android developer here. I know this is an unbelievably over-asked question, but everything I've tried from every other thread I could find DOES NOT WORK. No matter what. Either it will return null, or not work at all and throw an error on the line meant for getting a SharedPreferences object. Apologies if this is just a really tiny error.

Full stack trace and code below.

05-03 20:35:38.225 18928-18928/com.example.admin2.clicker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin2.clicker, PID: 18928
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.admin2.clicker/com.example.admin2.clicker.Home}: java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
    at com.example.admin2.clicker.Home.<init>(Home.java:50)
    at java.lang.Class.newInstance(Native Method)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

The code:

// Used to fetch SharedPreferences object
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    TextView xpText = (TextView) findViewById(R.id.xpText);
    TextView lvText = (TextView) findViewById(R.id.lvText);

    xpText.setText("Experience: 0/100");
    lvText.setText("Level 1");

    gameData = this.getSharedPreferences("gameData", MODE_PRIVATE);
}

// The line throwing the error + its context
{
    //boolean firstRun = gameData.getBoolean("f", false);
    boolean firstRun = true;
    if (firstRun) {
        SharedPreferences.Editor e = gameData.edit();
            e.putInt("xp", 0);
            e.putInt("mxp", 100);
            e.putInt("lv", 1);
            e.putInt("$", 0);
            e.putInt("$c", 0);
        e.apply();
    }
}

If you have any questions or need more info, let me know. I'm not sure of everything I need to provide. Thanks.

George Mulligan
  • 11,813
  • 6
  • 37
  • 50
cbryant02
  • 583
  • 4
  • 16
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Linh May 04 '16 at 01:51
  • When are you trying to use `gameData.edit()`? Your code seems incomplete. – Grender May 04 '16 at 01:54
  • @Grender gameData.edit() is used when checking if the app's on it's first run. There's an editor declared there (`SharedPreferences.Editor e = gameData.edit()`). – cbryant02 May 04 '16 at 02:20

2 Answers2

0

try this, you have to declare the file to save your data and after that you can only edit it.

SharedPreferences pref;
SharedPreferences.Editor editor;

pref = getSharedPreferences("MyPref", 0); // 0 - for private mode
editor = pref.edit();

i use commit() to update any data i want to save in SharedPreferences. As in your case is like

 editor.putInt("xp", 0);
 editor.putInt("mxp", 100);
 editor.putInt("lv", 1);
 editor.putInt("$", 0);
 editor.putInt("$c", 0);

 editor.commit();

i hope this will help you. Thanks.

R.Boo
  • 1
  • 1
  • Just tried initializing the editor variable before assigning it. Declaring the editor itself _does_ work, but trying to use putInt doesn't. Monitor throws `Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences$Editor.putInt(java.lang.String, int)' on a null object reference` – cbryant02 May 04 '16 at 02:28
0

You put the block of code to edit the preference in an initializer block which means it will be run before onCreate() where you actually assign gameData a value. That is what is leading to the NullpointerException.

As a rule you generally shouldn't do anything with an Activity until onCreate() is called. Especially if what you are doing requires a Context such as when dealing with preferences.

To fix this just move the code in the initializer block into the onCreate() method after you assign a value to gameData.

George Mulligan
  • 11,813
  • 6
  • 37
  • 50