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.