Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have created a class for working with SharedPreferences . In my Activity , I am trying to add an item and get the following error...

06-05 17:01:53.950: E/AndroidRuntime(3488): FATAL EXCEPTION: main
06-05 17:01:53.950: E/AndroidRuntime(3488): java.lang.NullPointerException
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.helpers.Prefs.init(Prefs.java:16)
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.helpers.Prefs.addStringProperty(Prefs.java:30)
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.usersadapter.SplashActivity$1.callback(SplashActivity.java:78)
06-05 17:01:53.950: E/AndroidRuntime(3488):     at com.xcxcxc.usersadapter.SplashActivity$1.callback(SplashActivity.java:1)

This is my code for handling the SharedPreferences...

import android.content.Context;
import android.content.SharedPreferences;
public class Prefs {
    public static final String STORAGE_NAME = "ApplicationPrefs";
    private static SharedPreferences settings = null;
    private static SharedPreferences.Editor editor = null;
    private static Context context = null;
    public static void init(Context cntxt) {
        context = cntxt;
    private static void init() {
        settings = context.getSharedPreferences(STORAGE_NAME, 0);
        editor = settings.edit();
    /* String values */
    public static void addStringProperty(String name, String value) {
        if (settings == null || editor == null) {
            init();
        editor.putString(name, value);
        editor.commit();
    public static String getStringProperty(String name) {
        if (settings == null || editor == null) {
            init();
        return settings.getString(name, null);

This is the code I am calling in my Activity...

Prefs.addStringProperty("client_id", "1JDkv9sdfj8sf63rjs");
                your context is null so NPE.so call first this init(Context cntxt) method and check in your if(null!=context){}
– Samir Mangroliya
                Jun 5, 2012 at 13:32

Looks like the Context is null. Set the Context before addStringProperty(..)

Prefs.init(getApplicationContext());
Prefs.addStringProperty("client_id", "1JDkv9sdfj8sf63rjs");

This statement must be causing the NPE.
How and where are you calling init(Context cntxt) method.

Unless the context object is initialized, you will get an NPE. Fix it first.

context is probably null, this is why you're getting null pointer exception:

private static void init() {
    settings = context.getSharedPreferences(STORAGE_NAME, 0);
    editor = settings.edit();

call public static void init(Context cntxt) first or handle the null value:

private static void init() {
    if(context != null){
       settings = context.getSharedPreferences(STORAGE_NAME, 0);
       editor = settings.edit();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.