Simbilli Dev
Simbilli Dev

Reputation: 237

Shared Preferences on a Null Object Reference Android

I am new to android developing maybe its a silly question but please help me. I am getting this error while trying to save an int value.

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

And here is my code

SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
String value = "com.example.app.value";
int i = prefs.getInt(value, 0);

And for write

prefs.edit().putInt(number, i).apply();

I just want to set a SharedPreferences and want to read it at the first and write it inside the Activity. How can I solve it?

EDIT

public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "myprefs";
public static final  String value = "key";

int i = sharedpreferences.getInt(value, 0);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
}
public void sendMessage(View view) {
    i += 1;    
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putInt(value, i);
    editor.apply();
}

I managed to save preferences with a different way but I couldn't manage to read it in MainActivity extends Activity class.

log :

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

Upvotes: 20

Views: 103435

Answers (4)

Black Diamond
Black Diamond

Reputation: 399

In my case this was the issues : https://stackoverflow.com/a/43160497/321015

I was calling shared prefereance before context was set. ideal way to call the context

newContextbase.getSharedPreferences(..

Upvotes: 5

Mehdi Abiyat
Mehdi Abiyat

Reputation: 21

This question was for the "Simplify Networking with RetroFit" android course. I was getting this error because i didnt have my BaseApplication class in the Android Manifest...Everything is good now

Upvotes: 2

Ashwin S Ashok
Ashwin S Ashok

Reputation: 3663

You can implement it like this

public class PreferenceClass {
    public static final String PREFERENCE_NAME = "PREFERENCE_DATA";
    private final SharedPreferences sharedpreferences;

    public PreferenceClass(Context context) {
         sharedpreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
    }

    public int getCount() {
         int count = sharedpreferences.getInt("count", -1);
         return count;
    }

    public void setCount(int count) {
         SharedPreferences.Editor editor = sharedpreferences.edit();
         editor.putInt("count", count);
         editor.commit();
    }

    public void clearCount() {
         SharedPreferences.Editor editor = sharedpreferences.edit();
         editor.remove("count");
         editor.commit();
    }
}

getCount() will return -1 if no value is set.

Upvotes: 2

Anand Singh
Anand Singh

Reputation: 5692

This is an example of SharedPreferences

To save name in SharedPreferences:

SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        Editor editor = sharedPreferences.edit();
        editor.putString(key, name);
        editor.apply();

To get name from SharedPreferences:

 SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        String name = sharedPreferences.getString(key, "default value");

For more details visit: http://developer.android.com/training/basics/data-storage/shared-preferences.html

Updated Code:

public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "myprefs";
public static final  String value = "key";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    int i = sharedpreferences.getInt(value, 0);
    //use the value of i where needed.
}
public void saveMessage(View view) {
    i += 1;    
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putInt(value, i);
    editor.apply();
}

Upvotes: 19

Related Questions