This Is The Code When I save My Value
public static final String
MyPREFERENCES = "MyPrefs" ;
public SharedPreferences sharedpreferences;
public static final String p = "pickLoc";
public static final String Phone = "dropLoc";
String pickLoc = pickupEt.getText().toString().trim();
String dropLoc = dropEt.getText().toString().trim();
sharedpreferences = getSharedPreferences(BookMyRide.this.MyPREFERENCES,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(p, pickLoc);
editor.putString(Phone, dropLoc);
editor.commit();
This Is The Code When i Want to Get Value
try{
sharedpreferences = getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);
sharedpreferences.getString("pickLoc" ,"");
sharedpreferences.getString("dropLoc","");
}catch (Exception e){
e.printStackTrace();
}
Then it Throws the Exception :
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
Thanks In Advance For Helping Me
Check this
The Context in your activity may be null
SharedPreference without PreferenceManager !!!
Check this sample
To save urValue in SharedPreferences:
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString(key, urValue);
editor.apply();
To get name from SharedPreferences:
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
String urValue = sharedPreferences.getString(key, "default value");
For more check this link