...on a null Object Reference

Asked

Viewed 772 times

3

Hello, I am at a time with this problem and I can not find solution, I have looked thousands of topics and I can not understand yet why this error appears to me.

Error

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.megll.mateusguedes.megll.Cadastro.CadastroPresenter.enviaDadosModel(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
at com.megll.mateusguedes.megll.Cadastro.CadastroActivity$1.onClick(CadastroActivity.java:52)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Code

package com.megll.mateusguedes.megll.Cadastro;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.megll.mateusguedes.megll.R;

import org.w3c.dom.Text;

public class CadastroActivity extends AppCompatActivity {

private EditText nomeCliente, telefone, celular, documento, email, endereco;

private Button btnEnviar, btnCancelar;

private CadastroPresenter cadastroPresenter;

public String val1, val2, val3, val4, val5, val6;

private static final String TAG = "CadastroActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cadastro);


    nomeCliente = (EditText) findViewById(R.id.nomeCliente);

    telefone = (EditText) findViewById(R.id.telefone);

    celular = (EditText) findViewById(R.id.celular);

    documento = (EditText) findViewById(R.id.email);

    email = (EditText) findViewById(R.id.endereco);

    endereco = (EditText) findViewById(R.id.endereco);

    btnEnviar = (Button) findViewById(R.id.btnEnviar);

    btnCancelar = (Button) findViewById(R.id.btnCancelar);

    btnEnviar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                cadastroPresenter.enviaDadosModel(nomeCliente.getText().toString(), telefone.getText().toString(), celular.getText().toString(),documento.getText().toString(), email.getText().toString(),  endereco.getText().toString());
        }
    });

}
}

I created the val1 to val6 strings to be passed to Cadastropresenter, but they were giving this same error so I decided to pass the value of the field directly to send it.

Way to shipDadosModel()

public void enviaDadosModel(String nomeCliente, String telefone, String celular, String documento, String email, String endereco){
    Cliente cliente = new Cliente();
    cliente.setId(UUID.randomUUID().toString());
    cliente.setNomeCliente(nomeCliente);
    cliente.setTelefone(telefone);
    cliente.setCelular(celular);
    cliente.setDocumento(documento);
    cliente.setEmail(email);
    cliente.setEndereco(endereco);
    String usuarioLogado = user.getEmail();
    cadastroModel.cadastrarUsuario(cliente, cliente.getId(), usuarioLogado);
}

I’m learning to develop android now, so forgive me if I don’t understand something.

2 answers

5

you’re tetando to make a method call on an empty null object, you need to instantiate at some point, it depends on your implementation.

private CadastroPresenter cadastroPresenter = new CadastroPresenter();

or

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

                cadastroPresenter = new CadastroPresenter(); 
                cadastroPresenter.enviaDadosModel(nomeCliente.getText().toString(),telefone.getText().toString(), celular.getText().toString(),documento.getText().toString(), email.getText().toString(),  endereco.getText().toString());
                }
            });
  • Our that juvenile mistake, vlw guy had not even seen that had not instantiated the class.

1

Your strings have to start with a value, otherwise they will be null

public String val1, val2, val3, val4, val5, val6;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cadastro);

       val1 = "";
       val2 = "";
       val3 = "";
       val4 = "";
       val5 = "";
       val6 = "";

    }
  • I was taking the value of the text fields and putting in them, the real problem was that I had not instantiated the class. But vlw so msm! haha

Browser other questions tagged

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