tiper / MaterialSpinner

Implementation of a Material Spinner for Android with TextInputLayout functionalities
Apache License 2.0
130 stars 29 forks source link

NPE when clearing selection if no adapter set #38

Open sebasira opened 3 years ago

sebasira commented 3 years ago

When I try to clear the spinnerSelection.

spinner.setSelection(-1)

And then I get:

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:344) at com.tiper.MaterialSpinner$DropDownAdapter.getCount(MaterialSpinner.kt:775) at com.tiper.MaterialSpinner.setSelection(MaterialSpinner.kt:131)

I get the list that would be used for the spinner from the server, and I was setting it to null when no connection or connection error. Now I set to an empty array and it works.

Maybe it's not a bug, but it took me a while to figure out that the problem was the null adapter. Maybe you can throw a different exception? Or clear it anyway even if it has no adapter