1
I’m developing a simple application to fill out a form, display the registrants and edit them. I’m using the one-to-many relationship where a patient can hold multiple photos, photos that are being saved through their path addresses in a string list. After saved in the bank, listed the registrants, when choosing a patient to edit it is the following error:
Attempt to invoke virtual method 'java.util.List br.ufrn.project.model.Foto.getImagem()' on a null object reference
It is not passing the patient object from one attempt to another, even if it is Serializing the class. Here is the Patient class:
public class Paciente implements Serializable {
@Expose
private int id;
@Expose
private String nome;
@Expose
private String cpf;
@Expose
public Foto foto;
@Expose
private String data;
getter e setter...
public class Foto implements Serializable {
@Expose
private List<String> imagem;
@Expose
private int id;
@Expose
private String status;
public List<String> getImagem() {
return imagem;
}
getter e setter...
On the list list I seek the patients from the bank and add in a list:
List<Paciente> list = pacienteDAO.buscar();//Retorna uma lista de pacientes salvo no banco
Inside the Adapter I am passing the patient object from the list screen to the edit screen.
Intent intent = new Intent(ListarActivity.this, MainActivity.class);
Paciente p = pacientes.get(i);
intent.putExtra("nome", pacientes.get(i).getNome());
intent.putExtra("cpf", pacientes.get(i).getCpf());
intent.putExtra("data", pacientes.get(i).getData());
intent.putExtra("id", pacientes.get(i).getId());
intent.getExtras().putSerializable("image", (Serializable) pacientes.get(i).getFoto().getImagem());
//intent.getExtras().putSerializable("status", pacientes.get(i).getFoto().getStatus());
startActivity(intent);
In the edit entry I rescue the Bundle with the object passed, where I place all image paths in a string list:
List<String> photos = new ArrayList<>();
photos = (List<String>) bundle.getSerializable("image");
//status = (Foto) bundle.getSerializable("status");
pac.setId(bundle.getInt("id"));
pac.setNome(bundle.getString("nome"));
pac.setCpf(bundle.getString("cpf"));
pac.setData(bundle.getString("data"));
pac.setFoto(pac.getFoto());
pac.setFoto(status);
nome.setText(pac.getNome());
cpf.setText(pac.getCpf());
mDisplayDate.setText(pac.getData());
countImage.setText(photos.size()+" selecionado(s)");
btAlterar.setVisibility(View.VISIBLE);
btSalvar.setVisibility(View.GONE);
For some reason he is not bringing the photos properly, returning Null. Main Activity save method:
public void salvarPaciente(){
Foto ft = new Foto();
pacienteDao = new PacienteDAO(this);
pac.setNome(nome.getText().toString());
pac.setCpf(cpf.getText().toString());
pac.setData(date);
ft.setStatus("Enviar");
ft.setImagem(caminhoDaImagem);
pac.setFoto(ft);
pacienteDao.inserirPaciente(pac);
pacienteDao.inserirFoto(ft);
Toast.makeText(this, "Paciente salvo com sucesso!", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(MainActivity.this, ListarActivity.class);
startActivity(myIntent);
}
On the bank side:
public long inserirFoto(Foto ft){
List<String> fotos = ft.getImagem();
ContentValues values = new ContentValues();
for(int i = 0; i < fotos.size(); i++) {
values.put("status","Enviado" );
values.put("imagem", fotos.get(i));
Log.d("TAG", fotos.get(i));
}return banco.insert("imagens", null, values);
}
public long inserirPaciente(Paciente paciente){
ContentValues values = new ContentValues();
values.put("nome", paciente.getNome());
values.put("cpf", paciente.getCpf());
values.put("data", paciente.getData());
return banco.insert("paciente", null, values);
}
Friend, if you try to send the patient without the images, are you going? For example, if you comment
intent.getExtras().putSerializable("image", (Serializable) pacientes.get(i).getFoto().getImagem());
and send, patient data (without photo data) arrive in other view?– Murilo Portugal
send yes, only when I put the pictures that gives error.
– Tecnologia da Net