Injecting Kotlin NullPointer Exception

I am new to kotlin , I am confusing below situation when I start Null Safety .

There's some data inconsistency regarding initialization (somewhere uninitialized is used that's available in the constructor).

Can anyone describe the situation in more detail?

+3


source to share


1 answer


An example adapted from Kotlin's discussion of exactly this :



class Foo {
    val c: String          // Non-nullable

    init {
        bar()
        c = ""             // Initialised for the first time here
    }

    fun bar() {
        println(c.length)  // Oh dear
    }
}

fun main(args: Array<String>) {
    Foo()
}

      

+4


source







All Articles