1

I have looked at this example https://stackoverflow.com/a/23024962/8341034 and I'm trying to recreate this in my code. So I've just used what they used in their example:

(this is before my onCreate)

public static final String MY_PREFS_NAME = "MyPrefsFile";
    SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();

then in my onCreate I have

   SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
        String restoredText = prefs.getString("text", null);

        example = findViewById(R.id.example);

        example.setText(restoredText);
        if (restoredText != null) {
            String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
            int idName = prefs.getInt("idName", 0); //0 is the default value.
        }

I manually put in a textview into my xml layout, which I called the textview example. I am trying to get the name saved in the shared prefs and put that into the textview. however when I run my app I get an error.

 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app.supermarketaislefinder/com.app.supermarketaislefinder.create}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2016)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
        at android.app.ActivityThread.access$700(ActivityThread.java:134)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4867)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:160)
        at com.app.supermarketaislefinder.create.<init>(create.java:63)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1319)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2007)

(The blue underline error is at line 63 which is at SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();

What have I done wrong?

p.s(what I am trying to achieve in the future is that before the app is closed, data from the many spinners in my screen are saved to shared prefs then when the app is reopened, these data values are put back into the spinners. but that's a different story)

  • I have read that however I'm still confused, as to why it's telling me it's on the line –  Sep 03 '18 at 06:20
  • `SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();` –  Sep 03 '18 at 06:20

0 Answers0