... on a null object reference

3

Hello, I'm at a time with this problem and I can not find a solution, I've already looked at thousands of topics and I still can not understand why this error appeared for 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 Strings val1 to val6 for them to be passed to CadastroPresenter, but they were giving this same error so I decided to pass the value of the field directly to the SendDataModel () but without success.

Method SendDataModel ()

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 sorry if I do not understand something.

    
asked by anonymous 27.09.2017 / 05:48

2 answers

5

You are about 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());
                }
            });
    
27.09.2017 / 05:57
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 = "";

    }
    
27.09.2017 / 06:25