The problem I have is trying to recover a item
of a ArrayList
that I filled with data from Mysql , because when I try to get it with the get()
it tells me the following:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get (int)' on a null object reference
LoadSpinner method:
public void CargarSpinner(JSONArray jsonArray){
ListaSpinner = new ArrayList<>();
for(int i=0;i<jsonArray.length();i++){
try {
JSONObject hijo = jsonArray.getJSONObject(i);
ListaPadres.add(hijo.getString("email_padre"));
ListaSpinner.add(hijo.getString("nombre_hijo"));
} catch (JSONException e) {
e.printStackTrace();
}
}
adaptador = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, ListaSpinner);
estudiantes.setAdapter(adaptador);
}
Here is where I try to get the ListArray item:
estudiantes = (Spinner)view.findViewById(R.id.spinner_estudiantes);
estudiantes.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), estudiantes.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
String email_padre = ListaSpinner.get(0).toString(); //Aqui es
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
What I'm doing is using a ArrayList
to fill the Spinner
with the names and the other ArrayList
to save your emails, but I already probe to get an item from both lists but the application stops.