1
resposta

.

Ao inserir novo aluno o app quebra, dizendo que tem null em Aluno PS: o código está em Kotlin

Da erro no aluno.setNome(nome) aluno.setTelefone(telefone) aluno.setEmail(email)

Segue class FormularioAlunoActivity

package br.com.alura.agenda.ui.activity

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import br.com.alura.agenda.R
import br.com.alura.agenda.dao.AlunoDao
import br.com.alura.agenda.model.Aluno

class FormularioAlunoActivity : AppCompatActivity() {

    private val campoNome: EditText
        get() {
            val campoNome: EditText = findViewById(R.id.activity_formulario_aluno_nome)
            return campoNome
        }

    private val campoTelefone: EditText
        get() {
            val campoTelefone: EditText = findViewById(R.id.activity_formulario_aluno_telefone)
            return campoTelefone
        }

    private val campoEmail: EditText
        get() {
            val campoEmail: EditText = findViewById(R.id.activity_formulario_aluno_email)
            return campoEmail
        }

    private val dao = AlunoDao()


    private var aluno: Aluno = Aluno()
        get() {

            val aluno: Aluno = intent.getSerializableExtra("aluno") as Aluno
            return aluno
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_formulario_aluno)
        title = "Novo aluno"

        configuraBotaoSalvar()

        if (intent.hasExtra("aluno")) {
            campoNome.setText(aluno.getNome())
            campoTelefone.setText(aluno.getTelefone())
            campoEmail.setText(aluno.getEmail())
        } else {
            aluno
        }
    }


    private fun configuraBotaoSalvar() {
        val botaoSalvarAluno: Button = findViewById(R.id.activity_formulario_aluno_botao_salvar)

        botaoSalvarAluno.setOnClickListener {
            preencheAluno()

            if (aluno.temIdValido()) {
                dao.edita(aluno)
            } else {
                dao.salva(aluno)
            }

            finish()
        }
    }
//
    private fun salva(alunoCriado: Aluno) {
        dao.salva(alunoCriado)

        finish()
    }

    private fun preencheAluno() {
        val nome = campoNome.text.toString()
        val telefone = campoTelefone.text.toString()
        val email = campoEmail.text.toString()

        aluno.setNome(nome)
        aluno.setTelefone(telefone)
        aluno.setEmail(email)
    }
}

e Segue class Aluno

package br.com.alura.agenda.model

import java.io.Serializable

class Aluno : Serializable {

    private var id: Int = 0
    private lateinit var nome: String
    private lateinit var telefone: String
    private lateinit var email: String


    constructor(nome: String, telefone: String, email: String) {
        this.nome = nome
        this.telefone = telefone
        this.email = email

    }

    constructor()


    override fun toString(): String {
        return nome
    }


    fun getId(): Int {
        return this.id
    }

    fun setId(id: Int) {
        this.id = id
    }

    fun getNome(): String {
        return nome
    }

    fun setNome(nome: String) {
        this.nome = nome
    }

    fun getTelefone(): String {
        return telefone
    }

    fun setTelefone(telefone: String) {
        this.telefone = telefone
    }

    fun getEmail(): String {
        return email
    }

    fun setEmail(email: String) {
        this.email = email
    }

    fun temIdValido(): Boolean {
        return (id > 0)
    }


}
1 resposta

Hey Odair, tudo bem ?

O problema é esse aqui:

    private var aluno: Aluno = Aluno()
        get() {

            val aluno: Aluno = intent.getSerializableExtra("aluno") as Aluno
            return aluno
        }

Quando você tenta pegar o aluno ele procura um aluno da intent, que num caso de aluno novo, não existe, o que gera seu erro.

O ideal é fazer intent.getSerializableExtra("aluno") as Aluno no onCreate para evitar esse problema.

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