Interface delegate in object

Hello everyone, at my new workplace, I have encountered a lot of refactoring of the old code(java and kotlin).

In the course of work, I found that it would be convenient for me to use the following construction

interface Base {
    val field1: String
}
class ImplGetter : Base {
    override val field1: String
        get() = ""
}

object Singleton : Base by instance {

    lateinit var instance: Base
}

iam try call some’s like that in Application#onCreate method -


        Singleton.instance = ImplGetter()
        println(Singleton.field1)

and fall in runtime with error

  java.lang.ExceptionInInitializerError
        at dto.ee.dmv.genius.DmvApplication.onCreate(DApplication.kt:17)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1223)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6734)
        at android.app.ActivityThread.access$1500(ActivityThread.java:256)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2090)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'dto.ee.Base dto.ee.Singleton.getInstance()' on a null object reference
        at dto.ee.Singleton.<init>(ApplicationConfig.kt:128)
        at dto.ee.Singleton.<clinit>(Unknown Source:2)

can anyone suggest a workaround? in real base interface i have 20+ fields, becouse implement with delegate most compfortable way for me.

I decided to rewrite my solution to the following

private lateinit var _instance: Base

fun init(instance: Base) {
    _instance = instance
}
object Singleton : Base by _instance

its work, but looks like Kludge