Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8331921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:34:02+00:00 2026-06-09T02:34:02+00:00

I am trying to save a integer value in the Shared preferences and load

  • 0

I am trying to save a integer value in the Shared preferences and load it again when the application is restarted.

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.

But I am getting a Null pointer exception when I try to do that.

I have also gone through this link!. But it doesn’t seem to be helping me.

Here is my code:

Main Activity:

    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();
    }

Menu Listener class:

    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;
    }

I am getting a Null pointer exception on the lines in the Menu Listener :

       mPrefs = getSharedPreferences("THEME", 0);
        mEditor = mPrefs.edit();    

My Logcat:

      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)

Can anyone please help me?

Thank You.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:34:03+00:00Added an answer on June 9, 2026 at 2:34 am

    One of the good ways is, you create a helper class to declare, save and retrive preferences. You can refer following code sample:

    public class PreferencesHelper {
    
    private SharedPreferences sharedPreferences;
    private Editor editor;
    
    public PreferencesHelper(Context context) {
    this.sharedPreferences = getPreferences(MODE_PRIVATE);    
    this.editor = sharedPreferences.edit(); }
    
    public String GetPreferences(String key) {
        return sharedPreferences.getString(key, "");
    }
    
    public void SavePreferences(String key, String value) {
    editor.putString(key, value);    
    editor.commit();  
    }
    }  
    

    In onCreate event of an activity, you can create of object of PreferencesHelper and get all saved preferences like:

    Prefs = new PreferencesHelper(getApplicationContext());  
    
    Prefs.GetPreferences("YOURARGUMENT");  
    

    and you can save the preferences like:

    Prefs.SavePreferences(key, value);  
    

    Hope this will help you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a integer named marbles, and am trying to save it into an
I am trying to save a BLOB created from an integer array (that is,
I am trying to import data into rails (3.1) and I have created this
I am trying to save a boolean database value into a map as follows
i'm trying to save a list of integers in my application by saving each
Let me clarify my question. I'm trying to get an integer value from @student.rooms.last,
im trying to save a row in my settings table, it works all fine
Im trying to save a simple script in netbeans to htdocs, but i only
When trying to save the database using boost serialization, I encounter the segfault that
I'm trying to save some data into an array but unfortunately all the data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.