Error returning objects saved in database between activites - Null object

Asked

Viewed 135 times

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);
}
  • 1

    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?

  • send yes, only when I put the pictures that gives error.

1 answer

1


Good, on android you can pass data using the parceable or the serializable. With the serializable I believe that the simplest way to do this is to send the whole object, since you are sending all attributes of the method separately, if you replace it with the object itself would be much simpler, see below.

In the view where you send the data, switch to send the whole object:

Intent intent = new Intent(this, MainActivity.class);
Paciente p = pacientes.get(i);
intent.putExtra("paciente", p);
startActivity(intent);

And in the edit view, to redeem do so:

Intent i = getIntent();
Paciente paciente = (Paciente)i.getSerializableExtra("paciente");

With the whole object you can now use the get methods of it to take the values of the attributes.

  • appears the following message: getextras() in Intent cannot be Applied when changing the screen code sending the data.

  • @Tecnologiadanet pardon, my mistake, in the view you send should use putExtra.

  • @Tecnologiadanet managed to send after the correction I mentioned?

  • Murilo, I’m sorry it took so long. I had to travel at the last minute and only I was able to pick up the code. Murilo, I believe the problem is in the SQL of saving the images. I am recovering all other data as indicated in the reply, only the pictures returns null.

Browser other questions tagged

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