简体   繁体   English

内联时的 Kotlin 空指针异常,但分离时不会

[英]Kotlin null pointer exception when inlined but not when separated

When collapsing code down into a single line, a null pointer exception starts getting thrown.当将代码折叠成一行时,开始抛出空指针异常。 But if separated into multiple variables, then it works just fine.但是如果分成多个变量,那么它工作得很好。 Noticed the issue when upgrading to kotlin 1.5.0 on android api 31.在 android api 31 上升级到 kotlin 1.5.0 时注意到了这个问题。

Code inline:内联代码:

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

Code separated:代码分离:

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

Any idea why?知道为什么吗?

Edit:编辑:

Found the cause of the issue.找到了问题的原因。 It was Gson producing null values when converting from json elsewhere in the codebase.当从代码库中的其他地方的 json 转换时,Gson 产生了空值。 Gson and Kotlin issue Gson 和 Kotlin 问题

Found the cause of the issue.找到了问题的原因。 It was Gson producing null values when converting from json elsewhere in the codebase.当从代码库中的其他地方的 json 转换时,Gson 产生了空值。 Gson and Kotlin issue Gson 和 Kotlin 问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM