title: how to get SharedPreference's null value in Android
body: <p>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. </p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>and i wanted to use the null value of sharedpreference so i made a code like this</p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>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: </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>line 195 is this: </p>
  
  <pre><code>SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
  </code></pre>
  
  <p>line 55 is this: </p>
  
  <pre><code>password = getPass("name", null);
  </code></pre>
  
  <p>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</p>
  
  <h2>EDITED PART</h2>
  
  <p>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 <strong>LogIn.java</strong>.</p>
  
  <h2>Menu.java</h2>
  
  <pre><code>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();
                          }
                      }
  
                  }
              });
  </code></pre>