Nullable value causes NullPointerException

val a:Boolean? = false
// if I set break point here and set a = null,
// then resume program will show NullPointerException
1.run {
    a?.let { false }   // line 104
}.apply {
    println(a)
}

Exception in thread “main” java.lang.NullPointerException
at TestKt.main(Test.kt:104)
at TestKt.main(Test.kt)

What causes this NullPointerException?

Perhaps the compiler can see that the value cannot* be null at that point, and so treats the .? safe-call like a normal . call?

(* Cannot — except for ‘impossible’ situations like changing it in a debugger…)

By the way, it would be helpful if you could post the code as text, not an image.

Thanks for your reply!

In real project, the location of “1” and “a” are two function, they will return the same type value, so I need “?.” for null safety, but when “a” return null, the NullPointerException will happen