android – getSelectedItem() on spinner returning null-ThrowExceptions

Exception or error:

I have a spinner array set with adapters the moment they are touched. However, when touched and an item has been selected, getSelectedItem() returns null.

I tried implementing if-else statements that check whether a spinner is null, but that does not work.

if (spinners[i-1][j]!=null) {

    // If nothing selected
    if (spinners[i-1][j].getSelectedItemPosition()==0) { 
          combined[i][j] = " ";
    } else {
          combined[i][j] = spinners[i-1][j].getSelectedItem().toString();
    }
} else {
    combined[i][j] = "null";
}

I need to extract the selected spinner item.

How to solve:

Check spinner has attached adapter or not.

if(spinners[i-1][j]!=null && spinners[i-1][j].getAdapter()!=null){
    //todo
    }

Leave a Reply

Your email address will not be published. Required fields are marked *