0

I'm new to Kotlin, and I'm trying to create an authentication short application. My application doesn't have any errors, but when I want to start the application it crashes.

The console debug system shows the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.menu.a9dli3.ui.modifyFragment.ModifyFragment.onCreate(ModifyFragment.kt:24)

Crash log sniped picture

The Kotlin view fragment code is here:

package com.menu.myapp.ui.modifyFragment
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.menu.a9dli3.R
import kotlinx.android.synthetic.main.register_activity.*

class ModifyFragment: Fragment() {

    private lateinit var modifyviewmodel: ModifyViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setOncreate(R.layout.fragment_modify_profile)
        login_id.setOnClickListener {

            val email = email_id.text.toString()
            val password = password_id.text.toString()

            Log.d("Login", "Attempt login with email/pw: $email/$password")
        }

        register_page_id.setOnClickListener {
            performRegister()
        }
    }

    private fun performRegister() {
        val email = email_id.text.toString()
        val password = password_id.text.toString()

        if (email.isEmpty() || password.isEmpty()) {
            Toast.makeText(context, "Please enter text in email/pw", Toast.LENGTH_SHORT ).show()
            return
        }
        Log.d("MainActivity", "Email is: $email")
        Log.d("MainActivity", "Password: $password")
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        modifyviewmodel =
            ViewModelProviders.of(this).get(ModifyViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_modify_profile, container, false)
        val templatemode: TextView = root.findViewById(R.id.text_slidemodify)
        modifyviewmodel.text.observe(this, androidx.lifecycle.Observer { templatemode.text = it })
        return root
    }
}

Please, I want some explanation and not just a solution.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sadiki Ayoub
  • 101
  • 1
  • 10

2 Answers2

1

Please follow the fragment lifecycle. You need to implement a click listener in the onViewCreated() method or the onCreateView() method as per lifecycle.

You have called a click listener before the create fragment view, and that was the big mistake. Without a create view, how can a button perform a click event?

Also you need to remove setOncreate(R.layout.fragment_modify_profile) from the onCreate() method. Please check the following code:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

    super.onViewCreated(view, savedInstanceState)

    login_id.setOnClickListener {

        val email = email_id.text.toString()
        val password = password_id.text.toString()

        Log.d("Login", "Attempt login with email/pw: $email/$password")
    }

    register_page_id.setOnClickListener {
        performRegister()
    }

}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rezaul Islam
  • 123
  • 1
  • 9
0

Your register_page_id is not inflated in onCreate() yet. The View starts to "exist" only after calling inflater.inflate().

So to set up your OnClickListener, move the line

register_page_id.setOnClickListener {
     performRegister()
}

right before return root in onCreateView().

If it still crashes, then replace register_page_id with root.findViewById<Button>(R.id.register_page_id )

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
muetzenflo
  • 5,653
  • 4
  • 41
  • 82