Attempt to invoke virtual method 'java.lang.String android.widget.Spinner.toString ()' on a null object reference

0

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?

    
asked by anonymous 12.07.2017 / 20:04

1 answer

0

The variables nomeS , contratadaS , tipoS and data must be null (so it gives the error quoted when trying to call toString() in them) because you are initializing them this way:

nomeS = (Spinner) rootView.findViewById(colaboradoresAmc);
contratadaS = (Spinner) rootView.findViewById(contratadasAmc);
tipoS = (Spinner) rootView.findViewById(spinnerTipo);
data = (EditText) rootView.findViewById(dataRealizada);

Where these parameters passed in the findViewById() methods should be references to their ID's in the layout XML. Ex: Rs.view_name

    
12.07.2017 / 20:29