Nullpointerexception error when calling a method

Asked

Viewed 139 times

0

public void addFormaPagamento() {
    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            boolean isInsertForma = dbApp.insertFormaPagamento(
                    spnForma.getSelectedItemPosition(),
                    spnParcelas.getSelectedItem().toString(),
                    edtvalor.getText().toString()
            );

            if (isInsertForma == true) {
                verifica_Add_Forma += 1;
                Toast.makeText(ActDetalheCheckout.this, "Forma de Pagamento Inserida com Sucesso", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(ActDetalheCheckout.this, "Falha os Inserir Forma de pagamento", Toast.LENGTH_LONG).show();
            }
        }
    });
}

You’re making a mistake on that line:

boolean isInsertForma = dbApp.insertFormaPagamento(

Error presented:

FATAL EXCEPTION: maijava.lang.NullPointerException
at app.teste1.ActDetalheCheckout$1.onClick(ActDetalheCheckout.java:65                                                              
  • 1

    Before calling an object method it is necessary to instantiate the class.

  • Where are you instantiating dbApp?

  • Only with the code shown you cannot guess which of the objects is null. I suggest putting each of these objects into variables and tracking their values by the debugger.

  • Only with this piece of code can’t be sure which part is causing the Nullpointer. It may mean that some of the parameters you are passing to the method insertFormaPagamento() is null or that dbApp is null or may have another cause. I suggest you try debugging your code, add a breakpoint in the error line and check the value of all arguments and follow the execution of the program step by step.

  • Guys, I managed to tidy up. I had forgotten to instantiate the object "dbApp", as the ramaral and Jorge had spoken. Thank you very much.

  • 1

    @Gabrielsouza You can post below the answer to your own question, there maybe if someday someone has the same problem, already see your answer.

Show 1 more comment

1 answer

1


Always when you launch a Nullpointerexception exception means you are trying to access a null object and/or variable.

In your case it is calling the insertFormaPath of the dbApp object that should not be instantiated.

Either put a test before to see if dbApp != null, or check the pq it is not being instantiated.

  • 3

    "Always when you throw a Nullpointerexception exception means you are trying to call a method from an empty object." - despite not being totally wrong, this margin to misinterpretations. The Nullpointerexception occurs when attempting to access a null object and/or variable. Not always an "empty object" is null.

  • Good Diego, that’s it.

  • 3

    I suggest you edit the answer by correcting this statement, so there is no margin for error in understanding.

  • 1

    "I suggest you edit the answer by correcting this statement, so there is no margin for error in understanding." - Done.

Browser other questions tagged

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