title: Android Null Pointer Exception for shared Preferences
body: <p>I am trying to save a integer value in the Shared preferences and load it again when the application is restarted.</p>
  
  <p>I have created a shared preferences in my main activity and trying to save integer value in a another Menu Listener class on menu item click. The Menu listener class is in a different package and the Menu Listener class extends the Main Activity.</p>
  
  <p>But I am getting a Null pointer exception when I try to do that.</p>
  
  <p>I have also gone through this <a href="https://stackoverflow.com/questions/5991289/sharedpreferences-null-pointer-exception">link</a>!. But it doesn't seem to be helping me.</p>
  
  <p>Here is my code:</p>
  
  <p>Main Activity:</p>
  
  <pre><code>    protected SharedPreferences mPrefs;
  protected SharedPreferences.Editor mEditor;
  protected Context  main_activityclass_context; //EDIT
  public static int count=0;
  
  @Override
  protected void onCreate(final Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
  
  
       mPrefs = this.getSharedPreferences("THEME", 0);
       mEditor = mPrefs.edit();
      }
  </code></pre>
  
  <p>Menu Listener class:</p>
  
  <pre><code>    case  mainActivity.MENU_ITEM_THEME_DARK:
      {
  
          mainActivity.count=1;
  
          mPrefs =main_activityclass_context.getSharedPreferences("THEME", 0); //EDIT
          mEditor = mPrefs.edit();
          mEditor.putInt("Theme_count", mainActivity.count).commit();
  
  
      break;
      }
  </code></pre>
  
  <p>I am getting a Null pointer exception on the lines in the Menu Listener :</p>
  
  <pre><code>       mPrefs = getSharedPreferences("THEME", 0);
          mEditor = mPrefs.edit();    
  </code></pre>
  
  <p>My Logcat:</p>
  
  <pre><code>      08-03 10:54:03.650: E/AndroidRuntime(31258): FATAL EXCEPTION: main
        08-03 10:54:03.650: E/AndroidRuntime(31258): java.lang.NullPointerException
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at               android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:153)
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at de.exb.ptpt.tablet.menu.TabletMenuListener.onMenuItemClick(TabletMenuListener.java:215)
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:144)
         08-03 10:54:03.650: E/AndroidRuntime(31258):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
         08-03 10:54:03.650: E/AndroidRuntime(31258):     at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:156)
         08-03 10:54:03.650: E/AndroidRuntime(31258):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
         08-03 10:54:03.650: E/AndroidRuntime(31258):     at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at android.widget.AbsListView$1.run(AbsListView.java:3168)
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at android.os.Handler.handleCallback(Handler.java:605)
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at android.os.Handler.dispatchMessage(Handler.java:92)
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at android.os.Looper.loop(Looper.java:137)
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at android.app.ActivityThread.main(ActivityThread.java:4424)
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at java.lang.reflect.Method.invokeNative(Native Method)
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at java.lang.reflect.Method.invoke(Method.java:511)
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        08-03 10:54:03.650: E/AndroidRuntime(31258):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Can anyone please help me?</p>
  
  <p>Thank You.</p>