Java – Null pointer exception while getting value from shared preferences

Null pointer exception while getting value from shared preferences… here is a solution to the problem.

Null pointer exception while getting value from shared preferences

In my main activity, I’m trying to get the value stored in the shared preference and the value is

stored in one of the fragments, but when I run the app, it gives me the NPE, and the code I’m trying to get the value is:

 spref=new SharedPref(getApplicationContext());
         get user data from session
          HashMap<String, Integer> selection = spref. GetPeerSelection();

 name
   int selec = selection.get(SharedPref.KEY_SelectionId);
             Save the Data in Database
           if(selec==1)
              Toast.makeText(getApplicationContext(),"1", Toast.LENGTH_LONG).show();

if(selec==0)
                  Toast.makeText(getApplicationContext(),"0", Toast.LENGTH_LONG).show();

My shared preference class is:

public class SharedPref {
     Shared Preferences
    SharedPreferences pref;
     Context _context;
     int PRIVATE_MODE = 0;
     Editor for Shared preferences
    Editor editor;
    private static final String PREF_NAME = "Settings";

 All Shared Preferences Keys

 User name (make variable public to access from outside)
    public static final String KEY_NAME = "name";
    public static final String KEY_MessageId = "msgid";
    public static final String KEY_SelectionId = "selectionid";
     Email address (make variable public to access from outside)
    public static final String KEY_HOPCOUNT = "hopcount";
    public static final String KEY_RANK = "rank";

public SharedPref(Context context){
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);

editor = pref.edit();
    }

/*  public void SaveRank(int rank)

{
         editor.putInt(KEY_RANK, rank);

}

*/
    public void SaveSettings(String name){

 Storing name in pref
        editor.putString(KEY_NAME, name);

 editor.putInt(KEY_HOPCOUNT, HopCount);
        commit changes
        editor.commit();
    }
  public void messageno(float msgno)

{
         editor.putFloat(KEY_MessageId, msgno);

editor.commit();
    }
  public void PeerSelection(int selection)

{
     editor.putInt(KEY_SelectionId, selection);

editor.commit();
  }

public HashMap<String, String> getUserName(){
        HashMap<String, String> user = new HashMap<String, String>();
         user name
        user.put(KEY_NAME, pref.getString(KEY_NAME, null));

 return user
        return user;
    }

public HashMap<String, Float> getMessageId(){
        HashMap<String, Float> id = new HashMap<String, Float>();
         Message No
        id.put(KEY_MessageId, pref.getFloat(KEY_MessageId,0.0f) );

 return user
        return id;
    }
    public HashMap<String, Integer> GetPeerSelection(){
        HashMap<String, Integer> selection = new HashMap<String, Integer>();
         Message No
        selection.put(KEY_SelectionId, pref.getInt(KEY_SelectionId,(Integer) null) );

 return user
        return selection;
    }

}

My log cat is:

12-10 11:47:16.624: E/AndroidRuntime(6029): FATAL EXCEPTION: main
12-10 11:47:16.624: E/AndroidRuntime(6029): java.lang.RuntimeException: Unable to start activity ComponentInfo{soft.b.peopleassist/soft.b.peopleassist.MainActivity}: java.lang.NullPointerException
12-10 11:47:16.624: E/AndroidRuntime(6029):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2084)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2111)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at android.app.ActivityThread.access$600(ActivityThread.java:134)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at android.os.Looper.loop(Looper.java:137)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at android.app.ActivityThread.main(ActivityThread.java:4666)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at java.lang.reflect.Method.invokeNative(Native Method)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at java.lang.reflect.Method.invoke(Method.java:511)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at dalvik.system.NativeStart.main(Native Method)
12-10 11:47:16.624: E/AndroidRuntime(6029): Caused by: java.lang.NullPointerException
12-10 11:47:16.624: E/AndroidRuntime(6029):     at soft.b.peopleassist.SharedPref.GetPeerSelection(SharedPref.java:98)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at soft.b.peopleassist.MainActivity.onCreate(MainActivity.java:140)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at android.app.Activity.performCreate(Activity.java:4510)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
12-10 11:47:16.624: E/AndroidRuntime(6029):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2048)
12-10 11:47:16.624: E/AndroidRuntime(6029):     ... 11 more

Solution

SharedPreference#getInt(String, int) takes the original int as a parameter. When null is passed as an Integer, it is automatically unboxed, but null is not a valid value for int< so that it throws a NullPointerException.

Related Problems and Solutions