Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must be initialized error in val variable

Tags:

android

kotlin

Following code report property must be initialized or be abstract error.

// MyApi.kt
interface MyApi {

    ...
}

// MyFetch.kt
class MyFetch {

    private val myApi: MyApi  // <- this line

    ...
}

It can use lateinit statement in mutable variable, but how should I predefined in val?

like image 665
ccd Avatar asked Dec 12 '25 20:12

ccd


1 Answers

You can use lazy, or make it nullable

private val myApi: MyApi? = null
like image 161
Egor Prokofyev Avatar answered Dec 14 '25 09:12

Egor Prokofyev