2

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) at 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) at com.playsimple.fitnessapp.ExerciseStartActivity.initExercise(ExerciseStartActivity.java:69) at com.playsimple.fitnessapp.ExerciseStartActivity.onCreate(ExerciseStartActivity.java:64) at android.app.Activity.performCreate(Activity.java:5248) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) at 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) at 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) at com.playsimple.fitnessapp.ExerciseStartActivity.initExercise(ExerciseStartActivity.java:69) at com.playsimple.fitnessapp.ExerciseStartActivity.onCreate(ExerciseStartActivity.java:64) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

user3730218
  • 49
  • 1
  • 7

2 Answers2

1

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) )

Community
  • 1
  • 1
Mohammed Ali
  • 2,758
  • 5
  • 23
  • 41
0

Try calling your sharedPreferences from other Activity as,

SharedPreferences settings = mainActivity.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
BDRSuite
  • 1,594
  • 1
  • 10
  • 15
  • What do you mean by calling from another Activity? Do you mean I should use that instead of SavingData.getRestTime(); – user3730218 Nov 12 '14 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. – BDRSuite Nov 12 '14 at 18:51