Look at the following code:
abstract class A {
init {
f()
}
abstract fun f()
}
class B(val p: () -> Unit) : A() {
override fun f() {
try {
p()
} catch (c: Throwable) {
println(c)
}
}
}
val b = B {
println("p() called")
}
Expecting p() called
will be printed, actual result is NPE:
java.lang.NullPointerException: Cannot invoke "kotlin.jvm.functions.Function0.invoke()" because "this.p" is null
Could you please explain, why can’t I access child constructor parameter from parent’s constructor? And how can I deal with this?