Android Spinner causes App to crash when scrolling reaches end of list-Collection of common programming errors

In one of my Activity, i have used the Spinner. i populate it with an array of SpinnerCountry class objects … its defined as following:

class SpinnerCounry {
    public SpinnerCounry(String spinnerText, String value) {
        this.spinnerText = spinnerText;
        this.value = value;
    }

    public String getSpinnerText() {
        return spinnerText;
    }

    public String getValue() {
        return value;
    }

    public String toString() {
        return spinnerText;
    }

    String spinnerText;
    String value;
}

I am binding the Spinner in a function that is below:

private void setCountriesSpinner() {

    //create array of countries!
    final SpinnerCounry items[] = new SpinnerCounry[233];
    items[0] = new SpinnerCounry("Abkhazia", "207");
    items[1] = new SpinnerCounry("Afghanistan", "1");
    items[2] = new SpinnerCounry("Albania", "103");
    items[3] = new SpinnerCounry("Algeria", "2");
              ....
              ....
              ....
    items[230] = new SpinnerCounry("Zambia", "154");
    items[231] = new SpinnerCounry("Zimbabwe", "155");

    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, items);
         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    Spinner s = (Spinner) findViewById(R.id.spinner);

    s.setAdapter(adapter);      

    s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView parent, View view,
                int position, long id) {

            //get the selected country's id to use later
            _countryId = items[position].getValue();

        }

        public void onNothingSelected(AdapterView parent) {
        }
    });
}

This works well … loads the countries in spinner & selects the correct country id when clicked/tapped.

BUT when i scroll the Spinner to the last country, it crashes the application. i looked at the LogCat but did not get whats wrong…

LogCat is as follows:

03-06 19:39:04.004: E/AndroidRuntime(1179): FATAL EXCEPTION: main
03-06 19:39:04.004: E/AndroidRuntime(1179): java.lang.NullPointerException
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:398)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:419)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:543)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:539)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.AbsListView.obtainView(AbsListView.java:1949)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1156)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.ListView.makeAndAddView(ListView.java:1756)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.ListView.fillDown(ListView.java:656)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.ListView.fillGap(ListView.java:620)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4408)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:3687)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.os.Handler.handleCallback(Handler.java:587)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.os.Looper.loop(Looper.java:132)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.app.ActivityThread.main(ActivityThread.java:4025)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at java.lang.reflect.Method.invoke(Method.java:491)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at dalvik.system.NativeStart.main(Native Method)

Any help is greatly appreciated. Thanks

  1. Your array contains 233 entries. But last entry [232] is empty, i.e. null. That is what is making your application crash.

    I’d recommend using an ArrayList instead of your current fixed array approach.

  2. You’ve created an array of 233 elements but you insert only 232 of them.

    Maybe, when you try to access the element at index 232, you get a null pointer exception.