holi-java
holi-java

Reputation: 30676

Kotlin NullPointerException occurrence

I'm new to , I'm confused the situation at below when I starting to Null Safety.

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

Could anyone describe the situation more in detailed?

Upvotes: 4

Views: 1987

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

Example adapted from a Kotlin discussion on 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()
}

Upvotes: 5

Related Questions