title: How to check whether shared preferences is null or not in android?
body: <p>In my apps for login and logout functionality, I am checking whether the shared preferences is null or not when activity starts, but I am still getting a NullPointerException.</p>
  
  <p>Here is my code for HomeActivity.java: </p>
  
  <pre><code>// Username And Password Editbox
  String eusername;
  String epassword;
  
  // Shared Preferences String declaration
  String spusername;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);     
  
      SharedPreferences spreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
      spusername=spreferences.getString("spusername","");
  
      if(!spusername.equals(""))
      {   
          Log.e("Spusername not null","--&gt;"+spusername);
      }
      if(spusername==null)
      {
          Log.e("Spusername null","--&gt;"+spusername);
      }
  }
  </code></pre>