Hannes Korte
05/09/2023, 11:32 AM// This code compiles, but leads to an NPE on a non-nullable type
class Foo {
val output = doSomething()
val input = "This value is not set yet"
fun doSomething(): String = input.uppercase()
}
The order of the class properties matters, as input
is still null
, when doSomething()
is called. This is trivial to see in this example, but tricky in more complex scenarios. Is this expected behavior? Couldn't the compiler notice the nullability issue?dmitriy.novozhilov
05/09/2023, 11:52 AMthis
in class initialization (contstuctors, property initializers, init blocks), even to members of the same classsciack
05/09/2023, 12:34 PMHannes Korte
05/09/2023, 12:45 PMthis
in class initialization" is good to know.peekandpoke
05/10/2023, 8:38 AMval output by lazy { doSomething() }