Ralph
Ralph

Reputation: 465

how to get SharedPreference's null value in Android

i have made a sharedpreference method like this in adding values and getting the stored values because i will also be using it in another class for updating the value.

public static void addPass(String key, String value, Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(key, value);
    editor.commit();
}
public static String getPass(String key, Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(key, null);
}

and i wanted to use the null value of sharedpreference so i made a code like this

password = getPass("name", null);

if ((password.equals(""))){
    if (s.toString().equals("1234")) {
        Intent Menu = new Intent(LogIn.this, Menu.class);
        LogIn.this.startActivity(Menu);

    } else {
        Toast.makeText(LogIn.this, "Incorrect Password", Toast.LENGTH_SHORT).show();
    }
}
else {
    realpass = getPass("password", LogIn.this);
    if (s.toString().equals(realpass)) {
        Intent Menu = new Intent(LogIn.this, Menu.class);
        LogIn.this.startActivity(Menu);

    } else {
        Toast.makeText(LogIn.this, "Incorrect Password", Toast.LENGTH_SHORT).show();
    }
}

if the password is null, the user should input 1234 as a password, else if it is null it will set what's stored in the sharedpreference as a password. but using this i will get an error like this:

02-06 18:25:25.267 2044-2044/com.secsys.gagacamaso.gagacamaso E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.secsys.gagacamaso.gagacamaso, PID: 2044
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:375)
        at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:370)
        at com.secsys.gagacamaso.gagacamaso.LogIn.getPass(LogIn.java:195)
        at com.secsys.gagacamaso.gagacamaso.LogIn$1.onTextChanged(LogIn.java:55)
        at android.widget.TextView.sendOnTextChanged(TextView.java:7991)
        at android.widget.TextView.setText(TextView.java:4345)
        at android.widget.TextView.setText(TextView.java:4199)
        at android.widget.EditText.setText(EditText.java:84)
        at android.widget.TextView.setText(TextView.java:4174)
        at com.secsys.gagacamaso.gagacamaso.LogIn$6.onClick(LogIn.java:136)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

line 195 is this:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

line 55 is this:

password = getPass("name", null);

thank you so much for anyone who will try to help me out on this, been trying to figure out how to have a default value and update it and store the password the whole day

EDITED PART

i was able to set the default password to 1234 but when i tried to change the password, i still get 1234 as the password, this is my code from another class the other one is named LogIn.java.

Menu.java

ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String oldpassword = oldpass.getText().toString();
                    String newpassword = newpass.getText().toString();
                    if (LogIn.password.equals("")) {
                        if (oldpassword.toString().equals("1234")) {
                            login.getPass("password",Menu.this);
                            login.addPass("password",newpassword,getApplicationContext());
                            Toast.makeText(Menu.this,"New password saved!",Toast.LENGTH_SHORT).show();
                        }
                        else {
                            Toast.makeText(Menu.this,"Old Password is not correct",Toast.LENGTH_SHORT).show();
                        }
                    }
                    else {
                        if (oldpassword.equals(LogIn.realpass)){
                            login.getPass("password",Menu.this);
                            login.addPass("password", newpassword, getApplicationContext());
                            Toast.makeText(Menu.this,"New password saved!",Toast.LENGTH_SHORT).show();
                        }
                        else{
                            Toast.makeText(Menu.this,"Old Password is not correct",Toast.LENGTH_SHORT).show();
                        }
                    }

                }
            });

Upvotes: 1

Views: 1547

Answers (1)

GoRoS
GoRoS

Reputation: 5375

This is because you are passing a null Context to the getPass method.

password = getPass("name", null); // <-- Null Context

Thus, the following code will crash as shown in the stacktrace:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); // <-- Null Context

Send a proper Application Context to it and it will work.

Furthermore, to get what you want you use the getString like this within the getPass method:

preferences.getString(key, "");

This will get an empty String if the prefs does not exist and your code will work as you're comparing with empty strings after that.

Upvotes: 2

Related Questions