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 4618508
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:16:37+00:00 2026-05-22T02:16:37+00:00

My first time using sharedPreferences and i can’t seem to get past this error.

  • 0

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.

In the DDMS I’m getting this:

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)

My code is this:

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

}

My submenu code in the dog class looks like this:

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

        }

Thanks

  • 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-05-22T02:16:38+00:00Added an answer on May 22, 2026 at 2:16 am

    getSharedPreferences() can only be called after onCreate() has been called on an Activity. Your Prefs class is strangely used in onOptionsItemSelected(). In general, you don’t instantiate activities yourself. If your Prefs class is not an actual Activity but a helper class for accessing preferences, pass a Context object to your methods, like this:

    public void editRegion(Context ctx, String sregion) {
        SharedPreferences settings = ctx.getSharedPreferences(PREFS_NAME,0);
        SharedPreferences.Editor ed = settings.edit();
        ed.clear();
        ed.putString(region,sregion);
        ed.commit();
    }
    

    and call it by:

    pob.editRegion(this, "uk");
    

    from your onOptionsItemSelected() method.

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

Sidebar

Related Questions

This is my first time using a JFrame. I can't get the window to
First time using this technique, seems that regardless what attributes I try to assign
This is my first time using stackoverflow for a question, I have read a
This is my first time using JQuery in any of my projects. I have
This is my first time using java to access databases, so I probably have
This is my first time using XSLT. I'm trying to create a file that
Cannot get this to work. First time using variables passed into functions. Unchecking radio
My first time using triggers. Can anyone please explain why this trigger won't work?
This is my first time using points and I seem to run into an
First time using this database because I need a type that's portable and so

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.