0
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = pref.edit();
    float highscore = pref.getFloat("highscore",0.0f);

As soon as I enter the activity that uses this part, the app crashes. According to logcat, that first line in this code block gives a nullpointerException. If it's important, this part comes after OnCreate, and I'm using Android Studio.

Logcat:FATAL EXCEPTION: main
                                                                                 Process: com.nonexistent.rs.guessthenumber, PID: 4388
                                                                                 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nonexistent.rs.guessthenumber/com.nonexistent.rs.guessthenumber.main}: java.lang.NullPointerException
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
                                                                                     at android.app.ActivityThread.access$900(ActivityThread.java:175)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:146)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5602)
                                                                                     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:1283)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                                                                                     at dalvik.system.NativeStart.main(Native Method)
                                                                                  Caused by: java.lang.NullPointerException
                                                                                     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:185)
                                                                                     at com.nonexistent.rs.guessthenumber.main.<init>(main.java:141)
                                                                                     at java.lang.Class.newInstanceImpl(Native Method)
                                                                                     at java.lang.Class.newInstance(Class.java:1208)
                                                                                     at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2297)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) 
                                                                                     at android.app.ActivityThread.access$900(ActivityThread.java:175) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:146) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5602) 
                                                                                     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:1283) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                                                                                     at dalvik.system.NativeStart.main(Native Method) 
07-14 10:17:04.616 6030-6030/com.nonexistent.rs.guessthenumber E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.nonexistent.rs.guessthenumber, PID: 6030
                                                                                 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nonexistent.rs.guessthenumber/com.nonexistent.rs.guessthenumber.main}: java.lang.NullPointerException
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
                                                                                     at android.app.ActivityThread.access$900(ActivityThread.java:175)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:146)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5602)
                                                                                     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:1283)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                                                                                     at dalvik.system.NativeStart.main(Native Method)
                                                                                  Caused by: java.lang.NullPointerException
                                                                                     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:185)
                                                                                     at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369)
                                                                                     at com.nonexistent.rs.guessthenumber.main.<init>(main.java:142)
                                                                                     at java.lang.Class.newInstanceImpl(Native Method)
                                                                                     at java.lang.Class.newInstance(Class.java:1208)
                                                                                     at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2297)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) 
                                                                                     at android.app.ActivityThread.access$900(ActivityThread.java:175) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:146) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5602) 
                                                                                     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:1283) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                                                                                     at dalvik.system.NativeStart.main(Native Method) 
Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
  • 1
    Can you point the exact line where you are getting null pointer? If possible add the full code. – Piyush Shrivastava Jul 14 '16 at 14:44
  • 1
    [Use LogCat to examine the Java stack trace associated with your crash](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). If you do not understand the trigger for the crash, post the entire Java stack trace. – CommonsWare Jul 14 '16 at 14:45
  • The first line of the code snippet I put gives a null pointer. I dont think I need to put the full code, because getting rid of the code snippet eliminates the problem. – superchampionoftheworld Jul 14 '16 at 14:47
  • Have you tried to [debug your code](https://developer.android.com/studio/debug/index.html) to determine which value is causing the null pointer exception? – buczek Jul 14 '16 at 14:49
  • can you post the entire .java file? What kind of class is it? – Bill Jul 14 '16 at 15:11

2 Answers2

0

Reason:

May be you are using this code from a fragment or on other thread. You Need to pass a activity context reference as an argument. The argument this is the problem.

Solution:

You need to pass the activity / context as the argument

    SharedPreferences pref = 
PreferenceManager.getDefaultSharedPreferences(Your_Activity_Name.this);
Christlin Panneer
  • 1,599
  • 2
  • 19
  • 31
-1

I solved this myself a long time ago, but then I realized that this was left unanswered. Apparently, I'm supposed to put all SharedPrefences code in onCreate.