nullpointerexception - Kotlin NullPointerException 发生

标签 nullpointerexception kotlin

我是新来的 , 当我开始 Null Safety 时,我对下面的情况感到困惑.

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



谁能更详细地描述一下情况?

最佳答案

示例改编自 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()
}

关于nullpointerexception - Kotlin NullPointerException 发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44529375/

相关文章:

java - 什么是NullPointerException,我该如何解决?

android - '无法访问主线程上的数据库,因为它可能会长时间锁定 UI' 另外,我使用协程访问了房间

kotlin - 使用 IntelliJ IDEA 时,Kotlin 中的 "An illegal reflective access operation has occurred"是什么意思?

function - Kotlin 使用数据类发送函数参数

java - fragment 中的 setListAdaptor 错误

jsf - FaceletViewHandlingStrategy$MethodRetargetHandlerManager$ArbitraryMethodRegargetHandler.retarget 处的 java.lang.NullPointerException

带有数组中对象的 Java NullPointerException

kotlin - 从 kotlin 中的集合中过滤掉非空值

android - android ContactsContract中的地址字段在哪里

使用静态 WebDriver 实例的 java.lang.NullPointerException