1

I'm getting a null pointer exception trying get a new activity to start when the user chooses a different locale. It seems to be occuring in the intent line as marked. (I'm a noob so simple explanations please!) Thanks

public void changeLocale(String locale){
    try{
    String l=locale;
     if(l.equals("uk")){
        this.isUk=true;
        Log.i(Log_tag,"isUk is true");
        //Toast.makeText(this, "UK region selected in Prefs", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Prefs.this, UK.class);//<---ERROR IS OCCURING HERE
        startActivity(intent);
    }else if(l.equals("eu")){
        this.isEu=true;
        Toast.makeText(this, "EU region selected", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Prefs.this, EU.class);
        startActivity(intent);
    }else if(l.equals("us")){
        this.isUs=true;
        Toast.makeText(this, "Us region selected", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Prefs.this, US.class);
        startActivity(intent);
    }else if (l.equals("")){
        Log.i(Log_tag,"no locale passed in");
        finish();
    }
    }catch (NullPointerException e){
        Log.i(Log_tag, "Null Pointer Error in changeLocale()"+e);
        finish();
    }
}

My logcat says this:

05-25 11:55:57.851: WARN/KeyCharacterMap(898): No keyboard for id 0
05-25 11:55:57.861: WARN/KeyCharacterMap(898): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

05-25 11:56:01.522: INFO/(898): UK selected in UsFM
05-25 11:56:01.543: INFO/(898): editRegion() started
05-25 11:56:01.642: INFO/(898): isUk is true
05-25 11:56:01.642: INFO/(898): Null Pointer Error in changeLocale()java.lang.NullPointerException

05-25 11:56:01.953: INFO/NotificationService(69): enqueueToast pkg=com.silifeform.android callback=android.app.ITransientNotification$Stub$Proxy@4380ba08 duration=0
05-25 11:56:01.995: INFO/ActivityManager(69): Starting activity: Intent { cmp=com.silifeform.android/.Us }

05-25 11:56:02.182: WARN/InputManagerService(69): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@438d8b88 (uid=10024 pid=898)

The whole class looks like this:

package com.silifeform.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class Prefs extends Activity {
    public static final String PREFS_NAME="LocalePrefs";
    private String region;
    public boolean isUk;
    public boolean isUs;
    public boolean isEu;
    private String Log_tag;

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


    @Override
    protected void onResume() {
        super.onResume();
        changeLocale(getRegion(this));
    }

    public void editRegion(Context cxt, String sregion) {
        setRegion(sregion);
        Log.i(Log_tag,"editRegion() started");
        SharedPreferences settings = cxt.getSharedPreferences(PREFS_NAME,0);
        SharedPreferences.Editor ed = settings.edit();
        ed.clear();
        ed.putString(this.region,sregion);
        ed.commit();
        changeLocale(sregion);
    }

    public String getRegion(Context cxt){
            SharedPreferences settings= cxt.getSharedPreferences(PREFS_NAME,0);
            String myRegion = settings.getString(this.region,"us");
            return myRegion;
        }

    public void setRegion(String region) {
        this.region = region;
    }


    public void changeLocale(String locale){
        try{
        String l=locale;
         if(l.equals("uk")){
            this.isUk=true;
            Log.i(Log_tag,"isUk is true");
            Log.i(Log_tag,l);
            //Toast.makeText(this, "UK region selected in Prefs", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Prefs.this, UkFuelMoney.class);
            startActivity(intent);
        }else if(l.equals("eu")){
            this.isEu=true;
            Toast.makeText(this, "EU region selected", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Prefs.this, EuFuelMoney.class);
            startActivity(intent);
        }else if(l.equals("us")){
            this.isUs=true;
            Toast.makeText(this, "Us region D selected", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Prefs.this, UsFuelMoney.class);
            startActivity(intent);
        }else if (l.equals("")){
            Log.i(Log_tag,"no locale passed in");
            finish();
        }
        }catch (NullPointerException e){
            Log.i(Log_tag, "Null Pointer Error in changeLocale()"+e);
            finish();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        SharedPreferences settings = getPreferences(0);
        SharedPreferences.Editor ed = settings.edit();
        ed.putString(region,region);
        ed.commit();

    }
}

Thanks Keno. The stack trace looks like this:

05-25 12:10:44.162: WARN/System.err(961): java.lang.NullPointerException
05-25 12:10:44.172: WARN/System.err(961):     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
05-25 12:10:44.182: WARN/System.err(961):     at android.content.ComponentName.<init>(ComponentName.java:75)
05-25 12:10:44.182: WARN/System.err(961):     at android.content.Intent.<init>(Intent.java:2302)
05-25 12:10:44.182: WARN/System.err(961):     at com.silifeform.android.Prefs.changeLocale(Prefs.java:68)
05-25 12:10:44.192: WARN/System.err(961):     at com.silifeform.android.Prefs.editRegion(Prefs.java:46)
05-25 12:10:44.192: WARN/System.err(961):     at com.silifeform.android.Us.onOptionsItemSelected(Us.java:347)
05-25 12:10:44.192: WARN/System.err(961):     at android.app.Activity.onMenuItemSelected(Activity.java:2096)
05-25 12:10:44.202: WARN/System.err(961):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:825)
05-25 12:10:44.202: WARN/System.err(961):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
05-25 12:10:44.202: WARN/System.err(961):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:813)
05-25 12:10:44.202: WARN/System.err(961):     at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:120)
05-25 12:10:44.212: WARN/System.err(961):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:884)
05-25 12:10:44.212: WARN/System.err(961):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
05-25 12:10:44.212: WARN/System.err(961):     at android.widget.ListView.performItemClick(ListView.java:3246)
05-25 12:10:44.222: WARN/System.err(961):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1635)
05-25 12:10:44.222: WARN/System.err(961):     at android.os.Handler.handleCallback(Handler.java:587)
05-25 12:10:44.222: WARN/System.err(961):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 12:10:44.232: WARN/System.err(961):     at android.os.Looper.loop(Looper.java:123)
05-25 12:10:44.232: WARN/System.err(961):     at android.app.ActivityThread.main(ActivityThread.java:4203)
05-25 12:10:44.242: WARN/System.err(961):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 12:10:44.242: WARN/System.err(961):     at java.lang.reflect.Method.invoke(Method.java:521)
05-25 12:10:44.242: WARN/System.err(961):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
05-25 12:10:44.252: WARN/System.err(961):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
05-25 12:10:44.252: WARN/System.err(961):     at dalvik.system.NativeStart.main(Native Method)
05-25 12:10:44.252: INFO/(961): Null Pointer Error in changeLocale()java.lang.NullPointerException
8
  • What's Prefs and is it, or Prefs.this null? Commented May 25, 2011 at 11:55
  • just print "l" to console & check what u r getting in it & also put u r log Commented May 25, 2011 at 11:57
  • Prefs is the class. I was wondering about that. My understanding is that the intent takes the activity where you are now and the activity you want to start, is that right? Commented May 25, 2011 at 11:59
  • Could you show snippet of code you have written for changeLocale? Commented May 25, 2011 at 11:59
  • 1
    In your catch block, do a e.printStackTrace() to see exactly where the null is originating from. Will be easier to track that way. Commented May 25, 2011 at 12:03

1 Answer 1

1

Your Prefs Activity is not populated as you probably have instantiated it yourself in com.silifeform.android.Us.onOptionsItemSelected() (I guess the implementation is still similar to the one you posted here). As I already pointed out in reply to th referred question of you, if the Prefs class is not instantiated (and managed) by Android, you cannot make assume it is working properly (especially all methods from Context) as it is not properly initiated. Again the same strategy applies, use a separate Context object you pass to changeLocale to fix this.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Stephan, yes indeed, the same problem again. I don't really understand how to use context. I've been trying to figure it out, but as you can see, I'm not there yet. I'll try with the context object, thanks.
It's not working cos I don't understand how to pass the context around. I've instantiated the Prefs object in the field variables, and tried to use it in com.silifeform.android.Us.onOptionsItemSelected(), but I'm really just guessing at this stage. Sorry, I really do appreciate your patience and help with this :)
As a quick face just do the same as the last time: Add a parameter of type Context to changeLocale and pass this from the call in onOptionsItemSelected() and use that new parameter as first parameter in the new Intent() calls.
Thank you Stephan. It's not working yet but I have to stop coding now so I'll get back to it tomorrow. Much appreciated :)
Ok, when you say "if the Prefs class is not instantiated (and managed) by Android, you cannot make assume it is working properly (especially all methods from Context) as it is not properly initiated." how do I instantiate the class so Android manages it? I've tried putting public Prefs pob; in the field variables but same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.