0

Shared Preferences are created as follows:

edt = sp.edit();
edt.putString("token_value", pojo.getKey());
edt.commit();

and I want to use this values in another class like this:

String key = sp.getString("token_value", "");

at this point I got an error like this:

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference

Please give me your valuable suggestions and thanks in advance...

Jason
  • 1,658
  • 3
  • 20
  • 51
  • please provide the whole method – Mina Fawzy Feb 23 '17 at 12:12
  • Hello sir this is complete method public static String PREF_NAME = "Login"; public static int PRIVATE_MODE=0; SharedPreferences sp; SharedPreferences.Editor edt; sp=getSharedPreferences(PREF_NAME,PRIVATE_MODE); edt=sp.edit(); edt.putString("token_value",pojo.getKey()); – Yakkala Naga Venkata Naresh Feb 23 '17 at 12:15
  • This code is not sufficient to understand the exact problem, Please provide the detailed code. – Shivam Feb 23 '17 at 12:16
  • Did you placed and accessed preferences in same thread ? If you are putting and accessing the preferences in different process, you might face issues.. – Umesh Chauhan Feb 23 '17 at 13:21
  • More details can be found here http://stackoverflow.com/a/4695567/3796784 – Umesh Chauhan Feb 23 '17 at 13:23
  • Please consider accepting an answer or post your own solution. It may be helpful for other with the same question. Thanks. – kodartcha Aug 17 '17 at 09:25

4 Answers4

0

Before retrieving the preferences did you initialize the sp object?

SharedPreferences sp = yourActivity.getPreferences(Context.MODE_PRIVATE);
String key = sp.getString("token_value", "");

The exception clearly states that your sp variable is a null reference and not pointing to any object.

Jason
  • 1,658
  • 3
  • 20
  • 51
Atif Rehman
  • 325
  • 1
  • 12
0

You are getting this error because you are passing null while getting a value. Change it to

String key = sp.getString("token_value", null);

like this and you will get the value.

Jason
  • 1,658
  • 3
  • 20
  • 51
Nitesh Mishra
  • 574
  • 1
  • 6
  • 14
  • Getting `""` is totally fine. That's the default string (empty) he will get if the variable does not exist on the `SharedPreferences`. Not null. – kodartcha Feb 23 '17 at 12:16
0

You SharedPreferences variable sp is declared but not initialised. Add this to your code:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(YourActivity.this);
String key = sp.getString("token_value","");
kodartcha
  • 1,063
  • 12
  • 23
0

To save variable at SharedPreferences

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
// make sure pojo.getKey() return value not null prefer make check here 
//if(pojo.getKey() != null)
editor.putString("token_value",pojo.getKey());
editor.apply();

retrieve any where you want

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("token_value", "");

do what ever you want with your string

Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
  • sorry to say still I got same error sir like this ----------- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference – Yakkala Naga Venkata Naresh Feb 23 '17 at 12:40
  • where you call getString method , activity , fragment , adapter – Mina Fawzy Feb 23 '17 at 12:48
  • any way this issue mean you pass wrong context , so replace this with Activity.this , or right context – Mina Fawzy Feb 23 '17 at 13:00
  • please check this one sir public class ConfigTask extends AsyncTask { ConfigCallback callback; private String emailId; Context con; SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(con); String name = sp.getString("token_value", ""); public ConfigTask(ConfigCallback callback, String emailId) { Log.d("++++++++++++++","+++++++++++"+ sp.getString("token_value","")); this.callback = callback; this.emailId = emailId; } – Yakkala Naga Venkata Naresh Feb 23 '17 at 13:18