1

Task: I am trying to get my app to read the country the user has selected from the preferences and then use the performClick() method on the radio button corresponding to the country the user has selected when the app starts again using setCountry().

Problem: Getting NullPointerException with performClick().

LOG CAT:

E/AndroidRuntime(23373): Caused by: java.lang.NullPointerException 01-04 15:20:27.025: E/AndroidRuntime(23373): at geminide.software.mpg.Main.setcountry(Main.java:256) 01-04


[setcountry() method] (This is where the problem occurs)

Exception caught on the perform click method that the preference is set to. If the preference was set to UK and then the app is re-opened it would be caught on the radiouk.performclick().

public void setcountry() {
    SharedPreferences pref = PreferenceManager
            .getDefaultSharedPreferences(this);
    String value = pref.getString("updates_country", "United States");

    if (value.equals("Europe")) {
        radioeu.performClick(); 
    } else if (value.equals("United States")) {
        radious.performClick();
    } else if (value.equals("United Kingdom")) {
        radiouk.performClick(); ... this is line 256, exception is caught because preferences is set to UK. 
    }
};

[Radio button that performClick() should be calling within the onCreate method]

radioeu.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            distancelabel.setText("Number of Kilometers:");
            gastanklabel.setText("  Number of Liters: ");
            unitlabel.setText("L/100km:");
        }
    });

[Preferences Class]

public class Settings extends PreferenceActivity {

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);

    // get the specified preferences using the key declared in
    // preferences.xml
    final ListPreference dataPref = (ListPreference) findPreference("updates_country");

    // get the description from the selected item
    dataPref.setSummary(dataPref.getEntry());

    dataPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object o) {
            // TODO Auto-generated method stub
            preference.setSummary(o.toString());

            return true;
        }
    });
}

}

[Settings.xml]

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory
    android:key="first_category"
    android:title="Preferences" >

    <ListPreference
        android:defaultValue="2"
        android:entries="@array/country"
        android:entryValues="@array/countryValues"
        android:key="updates_country"
        android:summary=""
        android:title="Default Country" />
</PreferenceCategory>

</PreferenceScreen>

Solution: Had defined the variables for the radio buttons both globally and locally. I removed the local declarations and the exception was gone.

Exception:

 RadioButton radioeu, radious, radiouk;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setcountry();

    RadioButton  radioeu = (RadioButton) findViewById(R.id.euradio);
    RadioButton  radious = (RadioButton) findViewById(R.id.usradio);
    RadioButton  radiouk = (RadioButton) findViewById(R.id.ukradio);

Fixed:

RadioButton radioeu, radious, radiouk;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setcountry();

    radioeu = (RadioButton) findViewById(R.id.euradio);
    radious = (RadioButton) findViewById(R.id.usradio);
    radiouk = (RadioButton) findViewById(R.id.ukradio);
Cœur
  • 37,241
  • 25
  • 195
  • 267
edinchev
  • 13
  • 5
  • You left out the most important part, the `setCountry` method where the error occurs. Don't forget to also show which one is line `256` in the `Main` activity. – user Jan 10 '13 at 18:48
  • It was actually in there but you probably missed it, I re-did the post and organized it better so its easier to read. setcountry method is now on the top of the post. radioeu.performClick() is line 256, i edited it in the original post so you can see it. – edinchev Jan 10 '13 at 19:22
  • My bad, I've missed it. I'm also assuming that your `R.layout.activity_main` file has those buttons it, no? And you also tried cleaning the project(menu Project -> Clean), maybe even an eclipse restart? – user Jan 10 '13 at 19:26
  • Yes, buttons are there and they work the way i intended them to if I wasn't trying to make it performClick() depending on the users preferences. Yes, I have cleaned the project and also restarted eclipse/computer. Off to school now so I will be on later to reply! Thanks for your help. – edinchev Jan 10 '13 at 19:30
  • I'm looking at your code from the previous edits: 1. I'm guessing you already done this but you have to look for those `RadioButtons` **before** you call `setCountry() in the `onCreate` method. 2. If you use `RadioButton radioeu = (RadioButton) findViewById(R.id.euradio);` you'll create local variables and you'll not initialize the *radioeu* `RadioButton` field in the Main class that you use in the `setCountry()` method. It should be like this `radioeu = (RadioButton) findViewById(R.id.euradio);`. – user Jan 10 '13 at 19:44
  • Wow...that was my problem I was defining what the buttons were multiple times, outside of the onCreate method and inside the oncreate method. I'll post how i fixed it on the main post. THANKS for your help! Can't believe I didn't notice that earlier. – edinchev Jan 11 '13 at 04:27
  • @Luksprog or edinchev, please post the solution in an answer of its own, thank you. – Cœur Jul 15 '18 at 09:49

0 Answers0