1

I am trying to save something to SharedPreferences using this code below:

public class SignupActivity extends Activity {

    String str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);

        str = PreferenceManager.getDefaultSharedPreferences(this).getString(
                "signedUp", null);

        SharedPreferences app_preferences = PreferenceManager
                .getDefaultSharedPreferences(this);

        String text = app_preferences.getString("signedUp", null);

        if(str.equals("YES")){
            startActivity(new Intent("com.mobi.job.scout.MyTabActivity"));
        }

        markAsSignedUp();

    }

    public void markAsSignedUp() {
        SharedPreferences.Editor localEditor = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext()).edit();
        localEditor.putString("signedUp", "YES");
        localEditor.commit();
        Log.d("Signed Up!", ":)");
    }

}

I have read and followed some tutorials on the internet but I'm getting the error below. This is the logcat:

10-23 01:47:27.061: E/AndroidRuntime(408): FATAL EXCEPTION: main
10-23 01:47:27.061: E/AndroidRuntime(408): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobi.job.scout/com.mobi.job.scout.SignupActivity}: java.lang.NullPointerException
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.os.Looper.loop(Looper.java:123)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-23 01:47:27.061: E/AndroidRuntime(408):  at java.lang.reflect.Method.invokeNative(Native Method)
10-23 01:47:27.061: E/AndroidRuntime(408):  at java.lang.reflect.Method.invoke(Method.java:521)
10-23 01:47:27.061: E/AndroidRuntime(408):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-23 01:47:27.061: E/AndroidRuntime(408):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-23 01:47:27.061: E/AndroidRuntime(408):  at dalvik.system.NativeStart.main(Native Method)
10-23 01:47:27.061: E/AndroidRuntime(408): Caused by: java.lang.NullPointerException
10-23 01:47:27.061: E/AndroidRuntime(408):  at com.mobi.job.scout.SignupActivity.onCreate(SignupActivity.java:28)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-23 01:47:27.061: E/AndroidRuntime(408):  ... 11 more

Please let me know what is wrong with the code I am using, I always end up getting force close. Thanks

1 Answer 1

8
str = PreferenceManager.getDefaultSharedPreferences(this).getString(
                "signedUp", null);

Your variable str is null because a value for "signedUp" has not had a value set before. Check for null in your if statement.

if(str != null && str.equals("YES")){

If will not continue past str != null if it is true.

Sign up to request clarification or add additional context in comments.

2 Comments

You could also change the default return value of getString from null to an empty String ""
Good point. Ideally you'd like to use a constant value for the default value for the preference, i.e. private static final String DEFAULT_SIGNEDUP_VALUE = "", or in my example = null. Either way would work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.