内联但分离时不发生 Kotlin 空指针异常

Kotlin null pointer exception when inlined but not when separated

将代码折叠成一行时,开始抛出空指针异常。但是如果分成多个变量,那么它就可以正常工作。在 android api 31.

升级到 kotlin 1.5.0 时注意到问题

内联代码:

embeddedBasket?.basket?.items?.items?.asSequence()?.map { it.productItem }?.toMutableList() ?: mutableListOf() // throws null pointer exception

代码分隔:

val productResponses = embeddedBasket?.basket?.items?.items // null
val asSequence = productResponses?.asSequence() // null
val map = asSequence?.map { it.productItem } // null
val toMutableList = map?.toMutableList() // null
val endList = toMutableList ?: mutableListOf() // Empty MutableList

知道为什么吗?

编辑:

找到问题的原因。当从代码库中其他地方的 json 转换时,Gson 会产生空值。 Gson and Kotlin issue

找到问题的原因。当从代码库中其他地方的 json 转换时,Gson 会产生空值。 Gson and Kotlin issue