Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am using SharedPreferences to get some saved values. I only get a null pointer exception on some devices. When I test the code in my own device I don't get any problem. But some of my user get the error.

Here is my class to get the value of SharedPreference:

public class SavingData {
     public static final String PREFS_NAME = "MyPrefsFile";
     public static MainActivity mainActivity;
public static int getRestTime() {
    // Restore preferences
    SharedPreferences settings = mainActivity.getSharedPreferences(PREFS_NAME, 0);
    int restTime = settings.getInt("resttime", defaultRestTime); // 0 is the default
                                                        // value
    return restTime;

In my main class I declare the mainActivity variable.

  SavingData.mainActivity = this;

Here is the error I get:

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.playsimple.fitnessapp/com.playsimple.fitnessapp.ExerciseStartActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198) android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) at android.app.ActivityThread.access$800(ActivityThread.java:139) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 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 com.playsimple.fitnessapp.data.SavingData.getDifficulty(SavingData.java:91) com.playsimple.fitnessapp.ExerciseStartActivity.initExercise(ExerciseStartActivity.java:69) com.playsimple.fitnessapp.ExerciseStartActivity.onCreate(ExerciseStartActivity.java:64) at android.app.Activity.performCreate(Activity.java:5248) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162) ComponentInfo{com.playsimple.fitnessapp/com.playsimple.fitnessapp.ExerciseStartActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) at android.app.ActivityThread.access$1500(ActivityThread.java:117) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:3683) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.playsimple.fitnessapp.data.SavingData.getDifficulty(SavingData.java:91) com.playsimple.fitnessapp.ExerciseStartActivity.initExercise(ExerciseStartActivity.java:69) com.playsimple.fitnessapp.ExerciseStartActivity.onCreate(ExerciseStartActivity.java:64) android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

You are creating an instance of your main activity here:

public static MainActivity mainActivity;

and then calling shared Prefs.. using that. Get the context object instead from your main activity and then call it. Also use this including MODE_PRIVATE argument:

SharedPreferences userDetails = context.getSharedPreferences("userdetails", MODE_PRIVATE);

See this detailed answer. Also see the docs for [Context getSharedPreferences](http://developer.android.com/reference/android/content/Context.html#getSharedPreferences(java.lang.String, int) )

from whereever you are calling the function, also get the context object, getRestTime(Context context), then context.getSharedPreferences(...). About the static thing i do not know if it should be used or not. But variable mainActivity seems to be the Problem. – Mohammed Ali Nov 12, 2014 at 19:02 When I do mainActivity.MODE_PRIVATE I get this warning: "The static field Context.MODE_PRIVATE should be accessed in a static way". Is it safe to do this Context.MODE_PRIVATE? – user3730218 Nov 12, 2014 at 19:13 What do you mean by calling from another Activity? Do you mean I should use that instead of SavingData.getRestTime(); – user3730218 Nov 12, 2014 at 18:50 savingData is not an activity in the code that you posted. Since it is a class, using getSharedPreferences with the preference name and mode should work. – vembutech Nov 12, 2014 at 18:51

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.