0

I am developing an Android Application, for keep me logged in functionality I am using SharedPreferences application started to crash.

This is the line that i add for Preferences;

SharedPreferences pref = getApplicationContext().getSharedPreferences("RS_Remember" , getApplicationContext().MODE_PRIVATE);

I am pretty sure that this line is causing error because it works fine if i remove it.

Error message i get says:

java.lang.Runtime: Unable to create application com.x.x.x : java.lang.NullPointerException.

Any idea abot what i shuold do?

Edit

public Activity activity;
    SharedPreferences pref;
    private RSCurrentUserManager()
    {
        super();

            pref = this.activity.getSharedPreferences("RallySpark_Remember",0);


    }


+05-09 18:02:56.221: E/AndroidRuntime(31760): FATAL EXCEPTION: main
+05-09 18:02:56.221: E/AndroidRuntime(31760):
 java.lang.RuntimeException: Unable to create application
 com.x.x.general.RSApplication:
 java.lang.NullPointerException
+05-09 18:02:56.221:
 E/AndroidRuntime(31760):   at
 com.x.x.general.RSCurrentUserManager.<init>(RSCurrentUserManager.java:93)
+05-09 18:02:56.221: E/AndroidRuntime(31760):   at com.x.x.general.RSCurrentUserManager.getInstance(RSCurrentUserManager.java:84)

I am not using Shared preferences in any other pleace in order to find the problem i removed everything execpt its defination

Yiğit Doğuş Özçelik
  • 2,763
  • 2
  • 16
  • 19
  • Try replacing `getApplicationCOntext()` with your `Activity context` – codeMagic May 09 '13 at 14:30
  • does getApplicationContext() returns null ? – Blackbelt May 09 '13 at 14:30
  • Instead of using ``getApplicationContext()`` just use the context variable, that you would need to pass from your Activity, ``context.getSharedPreferences()`` – Boardy May 09 '13 at 14:36
  • 2
    _Any idea abot what i shuold do?_ yes. You should read the stack trace created in your log at the time of the crash. This trace will point you at the possible points of failure. If you still need help after that, post the trace and relevant code. – mah May 09 '13 at 14:37
  • I already tried using Activit context but it didnt help. In order to understand if the getApplicationContext() is null or not i replaced code with `if(getApplicationContext() != null) { pref = getApplicationContext().getSharedPreferences("RS_Remember" , getApplicationContext().MODE_PRIVATE); }` it is still not working – Yiğit Doğuş Özçelik May 09 '13 at 14:40
  • Then please post full logcat and show where you are calling this – codeMagic May 09 '13 at 14:42

2 Answers2

1

If you are inside of an Activity method then you shouldn't need to even use Context. If you aren't or inside of an inner class or listener then replcae

SharedPreferences pref = getApplicationContext().getSharedPreferences("RS_Remember" , getApplicationContext().MODE_PRIVATE);

with

SharedPreferences pref = ActivityName.this.getSharedPreferences("RS_Remember" , getApplicationContext().MODE_PRIVATE);

Most of the time you will want to use Activity context instead of Application context. You can find many posts with good information about the two on SO. I'm pretty sure this is your problem but if it isn't then please post full logcat and show where this line is in your code.

This SO answer has a good explanation of the two

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Like you told i am not in a activity class, and this is what i tried before `Activity activity; SharedPreferences pref = activity.getSharedPreferences("RS_Remember" , activity.getApplicationContext().MODE_PRIVATE);` it gave me the same result. I tried the code that you but it didnt work still same error – Yiğit Doğuş Özçelik May 09 '13 at 14:52
  • pass `this` to your class where you try to use it and assing that to a `Context` variable then use that variable instead. If this doesn't work then please "post full logcat and show where you are calling this" – codeMagic May 09 '13 at 14:54
  • What is at line 93 of that class? Also, you don't appear to be assigning anything to `activity` – codeMagic May 09 '13 at 16:18
1

You can use SharedPreferences i.e.

SharedPreferences preference=null; 


@Override
protected void onCreate(Bundle arg0) {
preference= this.getSharedPreferences("RS_Remember", MODE_PRIVATE);
if(rememberMe.isChecked()){

                SharedPreferences.Editor editor = preference.edit();
                editor.putString("UserName", name.getText().toString());
                editor.putString("Password", password.getText().toString());


                // Commit the edits!
                editor.commit();


        }

}
Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30