title: NullPointerException in SharedPreferences Android
body: <p>My first time using sharedPreferences and i can't seem to get past this error. I have a submenu that is supposed to allow the user to set their region. This should open the correct region activity and be stored and recalled when the app is opened again. 
  I've been going round in circles many times so some of the code will be a bit wierd. I've been focusing on changing from US (default)to UK.</p>
  
  <p>In the DDMS I'm getting this:</p>
  
  <pre><code>05-13 11:22:39.344: ERROR/AndroidRuntime(960): java.lang.NullPointerException
  05-13 11:22:39.344: ERROR/AndroidRuntime(960):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
  05-13 11:22:39.344: ERROR/AndroidRuntime(960):     at com.silifeform.android.Prefs.editRegion(Prefs.java:29)
  05-13 11:22:39.344: ERROR/AndroidRuntime(960):     at com.silifeform.android.dog.onOptionsItemSelected(dog.java:344)
  </code></pre>
  
  <p>My code is this:</p>
  
  <pre><code>public class Prefs extends Activity {
  public static final String PREFS_NAME="LocalePrefs";
  private String region;
  public boolean isUk;
  public boolean isUs;
  public boolean isEu;
  
  @Override
  protected void onCreate(Bundle state) {
      super.onCreate(state);
  
      //restore prefs
      SharedPreferences settings= getSharedPreferences(PREFS_NAME,0);
      String myRegion = settings.getString(region,"us");
  
      this.region=myRegion;
      changeLocale(getRegion());
  }
  
  public void editRegion(String sregion) {
  // The error occurs here:
      SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);
      SharedPreferences.Editor ed = settings.edit();
      ed.clear();
      ed.putString(region,sregion);
      ed.commit();
  }
  
  public String getRegion(){
          SharedPreferences settings= getSharedPreferences(PREFS_NAME,0);
          String myRegion = settings.getString(region,"us");
          String gRegion=myRegion;
          return gRegion;
      }
  
  public void changeLocale(String locale){
      try{
      String l= locale;
      if(l==("us")){
          this.isUs=true;
          Toast.makeText(this, "Us region P selected", Toast.LENGTH_SHORT).show();
          Intent intent = new Intent(Prefs.this, dog.class);
          startActivity(intent);
      }
      if(l.equals("uk")){
          this.isUk=true;
          //Toast.makeText(this, "UK region selected", Toast.LENGTH_SHORT).show();
          Intent intent = new Intent(Prefs.this, cat.class);
          startActivity(intent);
      }
              }catch (NullPointerException e){
          //what to do here?
          finish();
      }
  }
  
  @Override
  protected void onStop() {
      super.onStop();
      SharedPreferences settings = getPreferences(0);
      SharedPreferences.Editor ed = settings.edit();
      ed.putString(region,region);
      ed.commit();
  
  }
  </code></pre>
  
  <p>My submenu code in the dog class looks like this:</p>
  
  <pre><code>public boolean onOptionsItemSelected(MenuItem item){
      Prefs pob = new Prefs();
      switch (item.getItemId()){
      //-------Options menu----------
      case R.id.about:
          //Toast.makeText(this, "About menu", Toast.LENGTH_SHORT).show();
          //showAbout();  
          return true;
      case R.id.locale:
          //Toast.makeText(this, "Locale menu", Toast.LENGTH_SHORT).show();
          return true;
  
      //-----Sub menu----------
  
                  case R.id.uk_item:
          Toast.makeText(this, "UK selected", Toast.LENGTH_SHORT).show();
          pob.editRegion("uk");
          pob.changeLocale("uk");
          finish();
          return true;
      case R.id.us_item:
          Toast.makeText(this, "US already selected", Toast.LENGTH_SHORT).show();
          pob.changeLocale("us");
          //finish();
          return true;
      default :
          return super.onOptionsItemSelected(item);
  
          }
  </code></pre>
  
  <p>Thanks</p>