Skip to content Skip to sidebar Skip to footer

Kotin Lateinit Var Not Initialized - Android Studio

I am working on a simple contact app that stores the contact's name, email, and number. Language: Kotlin Architecture: MVVM But I am getting an error: lateinit property addContact

Solution 1:

Instead of having your addContactViewModel as a lateinit property you can make it a lazy property, so that it gets initialized the first time you try to use it somewhere:

classAddContact : AppCompatActivity() {
    privateval addContactViewModel : AddContactViewModel by lazy {
        ViewModelProvider(this).get(AddContactViewModel::class.java)
    }

Solution 2:

You should initialize addContactViewModel in oncreate, not in setOnClickListener

Post a Comment for "Kotin Lateinit Var Not Initialized - Android Studio"