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

Asked

Viewed 101 times

0

I am trying to save the data of a spinner in a variable through findviewbyid, to later be saved in 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 from where the error is:

    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 spinner reference:

 private Spinner nomeS;
private Spinner contratadaS;
private Spinner tipoS;
private EditText data;
private View rootviewSalva;

And inside 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 someone please help me?

1 answer

0

The variables nomeS, contratadaS, tipoS and data should be null (so gives the error quoted when trying to call the toString() in them) because you are initiating them in 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 methods findViewById() should be references to their ID’s in the XML layout. Ex: R.id.name_da_view

  • I entered the R.id and the same error persists

  • Put the complete code, including the layout

Browser other questions tagged

You are not signed in. Login or sign up in order to post.