I'm trying to create a spinner with dynamic values.
It is giving an error when I try to set the adapter in spinner , below is the error and my code.
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
Click:
Sqlite sqlite = new Sqlite(getApplicationContext());
final ArrayList<Ocorrencia> arrayList = sqlite.getListaOcorrencia();
sqlite.close();
final Spinner spnocorrencia = view.findViewById(R.id.spnocorrencia);
OcorrenciaAdapter ocoAdapter = new OcorrenciaAdapter(getApplicationContext(), arrayList);
Log.i("getItem", String.valueOf(ocoAdapter.getItem(1)));
spnocorrencia.setAdapter(ocoAdapter); // erro está nessa linha
Adapter:
Context context;
ArrayList<Ocorrencia> arrayList;
public OcorrenciaAdapter(Context context, ArrayList<Ocorrencia> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
The error message says that I am trying to set a null value in the adapter, but my adapter returns value in " getCount()
" and also in " getItemId()
".