1

Main activity:

val turminha : Turma = this.abrirArquivo()

    recycler_view.layoutManager = LinearLayoutManager(this)
    recycler_view.adapter = AlunoAdapter(this,turminha.alunos)

    floatingActionButton.setOnClickListener({
        val i = Intent(this, ActivityAddAluno::class.java)
        i.putExtra("turma", turminha)
        startActivity(i)
    })

ActivityAddAluno

class ActivityAddAluno : AppCompatActivity() {
    var i = getIntent()
    private var turma = i.getSerializableExtra("turma") as Turma

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_aluno)

        mBtnSalvarAluno.setOnClickListener{
            var name = mEdtNomeAluno.text.toString()
            var matricula = mEdtMatriculaAluno.text.toString()
            toast("Nome: $name \n," +
                "Matricula: $matricula")

            turma.addAluno(Aluno(name, matricula))
        }
    }

Class Aluno class Aluno : Serializable {

var disciplinas: ArrayList<Disciplina>? = null
    private set
var nome: String? = null
var matricula: String? = null

constructor() {
    disciplinas = ArrayList()
}

constructor(nome: String, matricula: String) {
    this.nome = nome
    this.matricula = matricula
    disciplinas = ArrayList()
}

Class Turma

class Turma() : Serializable {

    val alunos: ArrayList<Aluno>

    init {
        alunos = ArrayList()
    }

    fun addAluno(aluno: Aluno) {
        alunos.add(aluno)
    }

When I try to access the other activity passing my Turma object in floatbutton, the app stops working. Logcat says the problem is in the code part:

var i = getIntent()
private var turma = i.getSerializableExtra("turma") as Turma

logcat:

FATAL EXCEPTION: main
Process: com.example.thial.estudandokotlin, PID: 3428
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.thial.estudandokotlin/com.example.thial.estudandokotlin.ActivityAddAluno}:
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)

How can i resolve this?

sisve
  • 19,501
  • 3
  • 53
  • 95
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Joshua Sep 22 '17 at 03:53
  • This explanation about nullpointer in java does not solve my case. I can not see where this problem is happening, could you tell me? – Thiago Pereira Sep 22 '17 at 04:00
  • `var i = getIntent()` is nullable. That is the reason. You call `i.getSerializableExtra("turma")` when `i` is `null`. It has said in the error. – Joshua Sep 22 '17 at 04:02
  • What change should I make to solve this? Everywhere I saw the examples were like this. – Thiago Pereira Sep 22 '17 at 04:04
  • 1
    Example are **NOT** like this. `getIntent()` should only be used when the Activity is ready. You are using `getIntent()` in constructor which it is not ready. You should take a look at Android activity life cycle. – Joshua Sep 22 '17 at 04:09
  • Thank u Joshua for the help. – Thiago Pereira Sep 22 '17 at 04:30

1 Answers1

0

Actually your problem is happening here as per your log cat messages.

var i = getIntent()
private var turma = i.getSerializableExtra("turma") as Turma

Here the Value "i" is null and on top of it your trying to take the getSerializableExtra() String.

Where exactly your calling this lines in ActivityAddAluno class ? It must be inside the onCreate function not in the constructor. (As per Joshua stated)

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //...
        var i = getIntent()
       if(i!=null)
       {
       private var turma = i.getSerializableExtra("turma") as Turma
       }

  }

Here is one of the example Switching between Activities along with data using Kotlin

King of Masses
  • 18,405
  • 4
  • 60
  • 77