Verifiquei que o Objeto aluno que o método public void preencheFormulario(Aluno aluno) recebe como parâmetro está com todos os fields preenchidos, mas na hora de setar estes valores nos campos do Formulário eu obtenho um erro de NullPointerException. Abaixo segue os fontes do FormularioActivity, FormularioHelper e a pilha de erro do java.
Código do meu FormularioActivity
package br.com.alura.listaalunos;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Toast;
import br.com.alura.listaalunos.dao.AlunoDAO;
import br.com.alura.listaalunos.modelo.Aluno;
public class FormularioActivity extends AppCompatActivity {
private FormularioHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
helper = new FormularioHelper(this);
Intent intent = getIntent();
Aluno aluno = (Aluno) intent.getSerializableExtra("aluno");
if (aluno != null) {
helper.preencheFormulario(aluno);
}
setContentView(R.layout.activity_formulario);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_formulario,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_formulario_ok :
Aluno aluno = helper.pegaAluno();
AlunoDAO alunoDAO = new AlunoDAO(this);
alunoDAO.insere(aluno);
alunoDAO.close();
Toast.makeText(FormularioActivity.this, "aluno " + aluno.getNome() +" Salvo", Toast.LENGTH_SHORT).show();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}
Código do FormularioHelper
package br.com.alura.listaalunos;
import android.widget.EditText;
import android.widget.RatingBar;
import br.com.alura.listaalunos.modelo.Aluno;
public class FormularioHelper {
private Aluno aluno;
private EditText campoNome;
private EditText campoEndereco;
private EditText campoTelefone;
private EditText campoSite;
private RatingBar campoNota;
public FormularioHelper(FormularioActivity activity) {
campoNome = (EditText) activity.findViewById(R.id.formulario_nome);
campoEndereco = (EditText) activity.findViewById(R.id.formulario_endereco);
campoTelefone = (EditText) activity.findViewById(R.id.formulario_telefone);
campoSite = (EditText) activity.findViewById(R.id.formulario_site);
campoNota = (RatingBar) activity.findViewById(R.id.formulario_nota);
this.aluno = new Aluno();
}
public Aluno pegaAluno() {
this.aluno.setNome(campoNome.getText().toString());
this.aluno.setEndereco(campoEndereco.getText().toString());
this.aluno.setTelefone(campoTelefone.getText().toString());
this.aluno.setSite(campoSite.getText().toString());
this.aluno.setNota(Double.valueOf(campoNota.getProgress()));
return this.aluno;
}
public void preencheFormulario(Aluno aluno) {
System.out.println("ID "+aluno.getId());
System.out.println("nome "+aluno.getNome());
System.out.println("ende "+aluno.getEndereco());
System.out.println("tele "+aluno.getTelefone());
System.out.println("site "+aluno.getSite());
System.out.println("nota "+aluno.getNota());
this.aluno = aluno;
campoNome.setText( this.aluno.getNome());
campoEndereco.setText(this.aluno.getEndereco());
campoTelefone.setText(this.aluno.getTelefone());
campoSite.setText(this.aluno.getSite());;
campoNota.setProgress((int) this.aluno.getNota());
}
}
StackTrace obtida com o erro em questão
04-20 13:32:12.031 10795-10795/br.com.alura.listaalunos E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.alura.listaalunos, PID: 10795
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.alura.listaalunos/br.com.alura.listaalunos.FormularioActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at br.com.alura.listaalunos.FormularioHelper.preencheFormulario(FormularioHelper.java:51)
at br.com.alura.listaalunos.FormularioActivity.onCreate(FormularioActivity.java:32)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)