Spinner: NullPointerException en ArrayAdapter.createViewFromResource

I create some Spinners by code like this :

    if (dyn_list_id[i]==null){
         String[] form_ids = db.getEntireColumn(list_table_name[i], "form_label_id");
         String[] spinnerArray = new String[form_ids.length];
         for (int k=0;k<form_ids.length;k++){
                 spinnerArray[k] = db.getFromLanguageTable(language_short_name, list_table_name[i], form_ids[k]);
                 System.out.println("Spinner Array "+spinnerArray[k]);
         }
         product_spinner[i] = new Spinner(viewToLoad.getContext());  //First Spinner
         product_spinner[i].setLayoutParams(lp_spinner);
         product_spinner[i].setBackgroundResource(R.drawable.spinner_selector);
         ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(viewToLoad.getContext(),  com.Orange.R.layout.my_spinner_textview, spinnerArray);
         spinnerArrayAdapter.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
         product_spinner[i].setAdapter(spinnerArrayAdapter);
         this.SpinnerFields.add(product_spinner[i]);

        linear_layout[i].addView(product_spinner[i]);
        tablerow_product[i].addView(linear_layout[i]);
        Themes_TableLayout.addView(tablerow_product[i],new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                                TableRow.LayoutParams.WRAP_CONTENT));
    }
    else {
    String[] spinnerA = new String[item_visible];
    int cont=0;
    for (int p=0;p<status_itemList.length;p++){
         if (status_itemList[p].trim().equals("1")){
               spinnerA[cont]=db.getFromLanguageTable(language_short_name, "pr_dyn_list_option", form_label_id_itemList[p]);
               System.out.println("Spinner dinamic array "+spinnerA[cont]);
               cont++;
               }  
    }

     product_spinner[i] = new Spinner(viewToLoad.getContext()); //second Spinner
     product_spinner[i].setLayoutParams(lp_spinner);
     product_spinner[i].setBackgroundResource(R.drawable.spinner_selector);

     ArrayAdapter spinnerAdapter = new ArrayAdapter(viewToLoad.getContext(),  com.Orange.R.layout.my_spinner_textview, spinnerA);
       spinnerAdapter.setDropDownViewResource(com.Orange.R.layout.multiline_spinner_dropdown_item);    product_spinner[i].setAdapter(spinnerAdapter);
    this.SpinnerFields.add(product_spinner[i]);
    linear_layout[i].addView(product_spinner[i]);
   tablerow_product[i].addView(linear_layout[i]); 
  Themes_TableLayout.addView(tablerow_product[i],new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
    TableRow.LayoutParams.WRAP_CONTENT));           
}

It appear fine on screen but when I click it I get this error :

15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime  java.lang.NullPointerException
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:329)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:325)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.widget.AbsListView.obtainView(AbsListView.java:1294)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.widget.ListView.onMeasure(ListView.java:1109)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.view.View.measure(View.java:8171)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.os.Handler.dispatchMessage(Handler.java:99)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.os.Looper.loop(Looper.java:123)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at android.app.ActivityThread.main(ActivityThread.java:4627)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at java.lang.reflect.Method.invokeNative(Native Method)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at java.lang.reflect.Method.invoke(Method.java:521)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
15:53:22.040    8716    com.Orange  ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

First Spinner added works fine, but for the second Spinner I get this error. I have no idea why I get a nullpointer at AdapterArray.createViewFromResources because I don't use this method. Where is my mistake?

preguntado el 31 de julio de 12 a las 14:07

What do you mean "for the second Spinner I get this error"? Could you provide more code? may be some test sample with activity and resourses? -

4 Respuestas

Tiene null values in the data lists/array that you supply to the adapter. ArrayAdapter llamará toString on this data for each row and if it is null a NullPointerException será arrojado

Asegúrate de no tener null Valores en spinnerA(I think that is the data for the second Spinner(!??!)).

Respondido 31 Jul 12, 14:07

I don't have any null valuea in spinnerA. The array has the correct values. - Gabrielle

@Gabrielle Are you sure? Did you print the values in the array after the for loop where you initialize it? - usuario

you are right. I made a mistake where I was printing the values of the array. Now I found that I have a value null, and that caused my error. Thanks for your help. - Gabrielle

hey i have this same problem. how can i check for the null values and remove them from array before populating it to spinner????? - Dr. ANDRO

@Dr.aNdRO You could iterate over the list and remove the null items from it. if you have an array then iterate over it to find how many entries are null and then construct a new array with that value and copy the non null items. - usuario

First spinner you use:

spinnerArrayAdapter.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);

second you use:

spinnerAdapter.setDropDownViewResource(com.Orange.R.layout.multiline_spinner_dropdown_item);

are you sure that both are the same/working correctly?

Respondido 31 Jul 12, 14:07

I put com.Orange.R.layout.multiline_spinner_dropdown_item for the second spinner after I got the error. I wanted to be sure the it use the correct xml. - Gabrielle

My problem was that the custom item layout and the dropdown item layout weren't using the same Id for the main textview; basically because I wanted to create a custom item, but maintain the default dropdown item.

Creating a custom dropdown item with the same textview id solved the problem.

Respondido el 27 de junio de 13 a las 01:06

Hey I have done it using this function, check for null values , don't add them to your list. This function will return a list array of all the values from a specified column of the table and NOT the null values.

public List<String> getList()
{
    List<String> list = new ArrayList<String>();


    Cursor cursor = sqlDb.query("tablename", new String[] {
            "someColumnName1" }, null, null, null, null, null);

        int i=0;
        for (cursor.moveToPosition(i); !cursor.isAfterLast(); cursor.moveToNext()) {
           if(cursor.getString(i) != null)   //in loop check for Null Values, add only Not null values
           {
               list.add(cursor.getString(i));
           }
        }


    return shopList;

}

Populate the spinner from the above method like this :

private void populateSpinnerList()
{
    List<String> list = getList();
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
            R.layout.some_layout, R.id.some_textview_in_layout, list);
    if (!list.equals(""))
        spinner.setAdapter(dataAdapter);
}

Funciona para mí como un encanto.

Respondido 01 Oct 13, 06:10

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.