Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

O app esta parando quando clico para editar um aluno após a modificação para salvar e editar telefones

Esta dando NullPointerException: Attempt to invoke interface method 'java.util.List com.paris.listaalunos.database.dao.TelephoneDAO.searchAllPhones(int)' on a null object reference.

Debuguei e vi que o método searchAllPhones esta retornando null. Não consegui achar o problema no código.

  private void loadStudent() {
        Intent date = getIntent();
        if (date.hasExtra(KEY_STUDENT)) {
            setTitle(TITLE_APPBAR_EDIT_STUDENT);
            student = (Student) date.getSerializableExtra(KEY_STUDENT);
            fillField();
        } else {
            setTitle(TITLE_APPBAR_NEW_STUDENT);
            student = new Student();
        }
    }

    private void fillField() {
        fieldName.setText(student.getName());
        fieldEmail.setText(student.getEmail());
        studentPhones = telephoneDAO.searchAllPhones(student.getId());
        for (Telephone telephone :
                studentPhones) {
            if (telephone.getType() == TelephoneType.TELEPHONE) {
                fieldTelephone.setText(telephone.getNumber());
            } else {
                fieldCellPhone.setText(telephone.getNumber());
            }

        }
    }

    private void fillForm() {

        String name = fieldName.getText().toString();
        String telephone = fieldTelephone.getText().toString();
        String cellPhone = fieldCellPhone.getText().toString();
        String email = fieldEmail.getText().toString();

        student.setName(name);
//        student.setTelephone(telephone);
//        student.setCellPhone(cellPhone);
        student.setEmail(email);

    }

    private void endForm() {
        fillForm();
        if (student.validId()) {
            studentDao.editStudent(student);
            String numberFixed = fieldTelephone.getText().toString();
            Telephone telephoneFixed = new Telephone(numberFixed, TelephoneType.TELEPHONE, student.getId());
            String numberCellphone = fieldCellPhone.getText().toString();
            Telephone cellphone = new Telephone(numberCellphone, TelephoneType.CELLPHONE, student.getId());
            for (Telephone telephone :
                    studentPhones) {
                if (telephone.getType() == TelephoneType.TELEPHONE) {
                    telephoneFixed.setId(telephone.getId());
                } else {
                    cellphone.setId(telephone.getId());
                }

            }
            telephoneDAO.update(telephoneFixed, cellphone);
            Toast.makeText(this, "Aluno Alterado com sucesso!", Toast.LENGTH_SHORT).show();

        } else {
            int studentId = studentDao.saveStudent(student).intValue();
            String numberFixed = fieldTelephone.getText().toString();
            Telephone telephoneFixed = new Telephone(numberFixed, TelephoneType.TELEPHONE, studentId);
            String numberCellphone = fieldCellPhone.getText().toString();
            Telephone cellphone = new Telephone(numberCellphone, TelephoneType.CELLPHONE, studentId);

            telephoneDAO.saveTelephone(telephoneFixed, cellphone);


            Toast.makeText(this, "Aluno salvo com sucesso", Toast.LENGTH_SHORT).show();
        }
        finish();
    }

@Dao public interface TelephoneDAO {

@Query("SELECT t.* FROM  Telephone t " +
        "WHERE t.studentId = :studentId LIMIT 1")
Telephone searchFirstTelephone(int studentId);

@Insert
void saveTelephone(Telephone... telephones);

@Query("SELECT * FROM  Telephone " +
        "WHERE studentId = :studentId")
List<Telephone> searchAllPhones(int studentId);

@Insert(onConflict = OnConflictStrategy.REPLACE)
void update(Telephone... telephones);

} segue o projeto no git: https://github.com/renanparis/Lista_Alunos_Alura.git

2 respostas
solução!

Oi Renan, eu acabei de responder a sua dúvida anterior e já indiquei o problema, dê uma olhada.

Opa Alex, Obrigado pela ajuda!!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software