0

I am creating an Android app and I am trying to use a SettingsManager class to read and write preferences. To use this, I have to pass context to this SettingsManager in order to use the SharedPreferences API. However, my code keeps giving me a NullPointerException. Can anyone tell me why?

Main.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_status);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
        setButton();
    }

...

public void setButton(){
    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            SettingsManager settings = SettingsManager.getInstance(getBaseContext());
            settings.getUserName();
        }
    });
}

SettingsManager.java

static Context context;

private SettingsManager(){
    sharedpreferences = context.getSharedPreferences(settingsfile, Context.MODE_PRIVATE);
}

public static SettingsManager getInstance(Context cntxt){
    if(instance == null){
        instance = new SettingsManager();
    }
    context = cntxt;
    return instance;
}

The NPE happens on this line:

sharedpreferences = context.getSharedPreferences(settingsfile, Context.MODE_PRIVATE);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Black Magic
  • 2,706
  • 5
  • 35
  • 58
  • post your logcat output please. – Mohammad Rahchamani Nov 27 '14 at 12:10
  • 3
    Please do not hold onto random `Context` objects in static data members -- you are leaking memory in this case. `SettingsManager` is largely pointless (`SharedPreferences` are already cached by the framework), but if you insist on keeping it, hold onto `getApplicationContext()` in the static data member, please. – CommonsWare Nov 27 '14 at 12:12
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Nov 27 '14 at 12:13

2 Answers2

2

it's not resulting in a NPE. What you are doing wrong is accessing context before assigning it. Change your code like

private SettingsManager(Context context){
   context = cntxt;
   sharedpreferences = context.getSharedPreferences(settingsfile, Context.MODE_PRIVATE);
}


public static SettingsManager getInstance(Context cntxt){
    if(instance == null){        
        instance = new SettingsManager(cntxt);
    }
    return instance;
}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

You can also get the context from the view object.

SettingsManager settings = SettingsManager.getInstance(v.getContext());
cgew85
  • 427
  • 3
  • 9