I'm trying to save data from a spinner to a variable via findviewbyid, and then it's saved to SQLITE. The data is, name, type, third, and date. But when I try to set this data, it is generating the following error:
FATAL EXCEPTION: main
Process: com.example.gerdaumanagement.gerdaumanagement, PID: 5657
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.widget.Spinner.toString()' on a null object reference
at com.example.gerdaumanagement.gerdaumanagement.adicionarAmc.salvarAmc(adicionarAmc.java:238)
at com.example.gerdaumanagement.gerdaumanagement.adicionarAmc.salvarRespostas(adicionarAmc.java:229)
at com.example.gerdaumanagement.gerdaumanagement.tipoMensal$1.onClick(tipoMensal.java:57)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Follow the code where you're giving the error:
public void salvarRespostas(ArrayList<Integer> respostas) {
amcFeita.respostas = respostas;
amcFeita.setNome(nomeS.toString());
amcFeita.setTipo(tipoS.toString());
amcFeita.setTerceira(contratadaS.toString());
amcFeita.setData(data.getText().toString());
db_funcao db = new db_funcao(getContext());
db.inserirAmc(amcFeita);
Toast.makeText(getActivity(), "AMC inserida com sucesso!", Toast.LENGTH_SHORT).show();
}
I also have these global variables to save the spinners reference:
private Spinner nomeS;
private Spinner contratadaS;
private Spinner tipoS;
private EditText data;
private View rootviewSalva;
And within the oncreate I save them:
nomeS = (Spinner) rootView.findViewById(colaboradoresAmc);
contratadaS = (Spinner) rootView.findViewById(contratadasAmc);
tipoS = (Spinner) rootView.findViewById(spinnerTipo);
data = (EditText) rootView.findViewById(dataRealizada);
rootviewSalva = rootview;
Can anyone help me please?