NRE from an "uninitialized this" in constructor


#1

Hi there,

I’m relatively new to Kotlin, and its handling of nulls / prevention of NREs looks really cool. But I’m reading the section that describes the four specific scenarios where one can still get NREs, and I’m having trouble visualizing the last one:

“There’s some data inconsistency with regard to initialization (an uninitialized this available in a constructor is used somewhere)”

When would this happen?


#2
class Foo {
    val c: String

    init {
        bar()
        c = ""
    }

    fun bar() {
        println(c.length)
    }
}

#3

Ah, of course! Thank you for the example.