在包含NULL的ArrayList上使用映射{}时使用NPE

时间:2019-03-14 08:40:00

标签: java dictionary kotlin nullpointerexception null

我要

  

java.lang.NullPointerException:尝试调用虚拟方法   空对象引用上的'float java.lang.Number.floatValue()'

使用以下代码:

val localHistory: ArrayList<Float> = ArrayList<Float>()
...    
val strHistory = localHistory.map { value -> decoration.decoratedValue(ref.format, value) }

我刚刚了解到ArrayList可能包含空值(oookay)。这意味着映射转换闭包中的 value 可能为NULL,不是吗?

但这不能是因为 value 不是可选类型,并且编译器说if(value!= null)始终为true。

所以问题是在这种情况下如何避免NPE?

3 个答案:

答案 0 :(得分:6)

我建议将您从Java收到的 [[[[-0.00476191 -0.00361736 0. 0.25361738 -0.07576995 0.03405379 0.40910327] [ 0.21594621 0.04544836 0. 0.28788495 0.30689242 -0.13025634 0.05074273] [ 0.46358964 0.19925728 0. -0.09778295 0.26563603 0.34778297 -0.02014329] [-0.01515752 0.3534766 0. 0.32857144 -0.00361736 0.67142856 0.25361738] [ 0.25756338 0.03405379 0. 0.21594621 0.3787817 -0.05689242 0.6212183 ] [ 0.30689242 0.203077 0. 0.796923 0.19925728 0.40103063 -0.09778295] [ 0.5989694 0.34778297 0. -0.01515752 0.68680996 0.26515752 0.66190475] [-0.00361736 1.0047619 0. 0.59089667 0.03405379 1.0757699 0.21594621] [ 0.712115 -0.05689242 0. 0.30689242 0.53641033 0.05074273 1.1302563 ] [ 0.19925728 0.7343639 0. 0.93230265 0.34778297 0.64652336 -0.01515752] [ 1.0201433 0.26515752 0. 0.24638264 0.33809522 0.50361735 -0.07576995] [ 0.2840538 0.40910327 0. 0.04544836 0.19310758 0.28788495 0.5568924 ] [-0.13025634 0.30074272 0. 0.44925728 0.06769729 0.15221705 0.26563603] [ 0.59778297 -0.02014329 0. 0.3534766 0.5151575 0.32857144 0.24638264] [ 0.67142856 0.50361735 0. 0.2840538 0.7424366 0.4659462 0.3787817 ] [ 0.19310758 0.6212183 0. 0.203077 0.30074272 0.796923 0.44925728] [ 0.40103063 0.15221705 0. 0.59778297 0.31319004 0.23484248 0.68680996] [ 0.5151575 0.66190475 0. 1.0047619 0.50361735 0.59089667 0.2840538 ] [ 1.0757699 0.4659462 0. 0.19310758 0.95455164 0.5568924 0.53641033] [ 0.30074272 1.1302563 0. 0.7343639 0.15221705 0.93230265 0.59778297] [ 0.64652336 0.23484248 0. 0.5151575 -0.00476191 0.49638262 0.33809522] [ 0.75361735 -0.07576995 0. 0.40910327 0.7159462 0.04544836 0.44310758] [ 0.28788495 0.8068924 0. 0.55074275 0.46358964 0.69925725 0.06769729] [ 0.40221703 0.26563603 0. -0.02014329 0.48484248 0.3534766 0.7651575 ] [ 0.32857144 0.49638262 0. 0.75361735 0.25756338 0.5340538 0.7424366 ] [ 0.7159462 0.3787817 0. 0.6212183 0.8068924 0.203077 0.55074275] [ 0.796923 0.69925725 0. 0.40221703 0.5989694 0.84778297 0.31319004] [ 0.48484248 0.68680996 0. 0.66190475 0.49638262 1.0047619 0.75361735] [ 0.59089667 0.5340538 0. 0.7159462 0.712115 0.44310758 0.95455164] [ 0.8068924 0.53641033 0. 1.1302563 0.69925725 0.7343639 0.40221703] [ 0.93230265 0.84778297 0. 0.48484248 1.0201433 0.7651575 -0.00476191] [ 0.74638265 0.33809522 0. -0.07576995 0.7840538 0.40910327 0.9659462 ] [ 0.04544836 0.6931076 0. 1.0568924 -0.13025634 0.80074275 0.46358964] [ 0.94925725 0.06769729 0. 0.26563603 1.0977829 -0.02014329 0.7348425 ] [ 0.3534766 1.0151576 0. 0.74638265 0.67142856 1.0036174 0.25756338] [ 0.7840538 0.7424366 0. 0.3787817 0.6931076 0.6212183 1.0568924 ] [ 0.203077 0.80074275 0. 0.94925725 0.40103063 0.65221703 0.5989694 ] [ 1.0977829 0.31319004 0. 0.68680996 1.0151576 0.66190475 0.74638265] [ 1.0047619 1.0036174 0. 0.7840538 1.0757699 0.9659462 0.712115 ] [ 0.6931076 0.95455164 0. 0.53641033 0.80074275 1.1302563 0.94925725] [ 0.7343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 0.2 ] [ 0.1 0.1 0. 0.2 0.1 0.1 0.2 ] [ 0.2 0.1 0. 0.2 0.2 0.1 0.1 ] [ 0.2 0.2 0. 0.1 0.2 0.2 0.1 ] [ 0.1 0.2 0. 0.1 0.1 0.2 0.2 ] [ 0.1 0.1 0. 0.2 0.1 0.1 0.2 ] [ 0.2 0.1 0. 0.2 0.2 0.1 0.1 ] [ 0.2 0.2 0. 0.1 0.2 0.2 0.1 ] [ 0.1 0.2 0. 0.1 0.1 0.2 0.2 ] [ 0.1 0.1 0. 0.2 0.1 0.1 0.2 ] [ 0.2 0.1 0. 0.2 0.2 0.1 0.1 ] [ 0.2 0.2 0. 0.8479438 0.67317617 0.5581815 0.1778345 ] [-0.9215721 1.5896183 0. 0.6099795 0.5955366 -0.46569395 -0.8461083 ] [ 1.6129647 1.4244858 0. 0.5209342 0.17585325 -0.8687666 1.7872683 ] [ 1.3389692 0.8533131 0. -0.00590521 -0.7195761 1.6236191 1.1828533 ] [ 1.1838211 0.6728102 0. -0.785988 1.2751837 1.1616383 0.933811 ] [ 0.4684658 0.2719049 0. 1.2093123 0.66612804 0.66964823 0.55971766] [ 0.17104894 -1.0688283 0. 0.6494252 0.6844874 0.66586125 0.01329695] [-1.2607187 -0.22749203 0. -0.8741171 -0.9443728 -0.9659323 -0.03422031] [-0.0364061 0.54829746 0. 0.6263525 0.66758543 0.04167109 -0.11780822] [ 0.48400337 0.4685324 0. -0.04594427 0.02469592 -0.3487326 0.08831279] [ 0.4161314 0.23332608 0. -0.13553022 -0.31008872 0.04969648 0.5674252 ] [ 0.36492363 -0.07475745 0. -0.03859219 0.2016789 -0.39845943 -0.07058203] [-0.08173721 0.1720942 0. 0.02323131 0.07122216 0.07469177 0.12792486] [-0.24689877 0.196296 0. 0.5564647 0.535513 0.22528338 -0.37152448] [-1.7235181 -1.8204601 0. -1.5040898 -1.8099409 -1.8550183 -1.1855855 ] [-1.6341007 -1.3448519 0. -1.6656716 -1.6564709 -1.2735447 -1.3357594 ] [-1.2829769 -1.2869868 0. -1.6657944 -1.4066424 -1.4230443 -1.4196167 ] [-1.3691044 -1.656098 0. -1.4339573 -1.5685135 -1.633306 -1.4437945 ]]]] 中的每个参数标记为可为空(除非您当然知道永远不能为Collection

null

然后,您可以过滤出val floats: List<Float?> = someJavaClass.getFloats() 并执行地图操作:

null

请记住,每个val newFloats: List<Float> = floats .filter { it != null } .map { it + 1 } filter(等等)操作都将迭代整个集合并每次都创建一个新集合。最好使用map,它们是懒惰地迭代的。

如评论中所述,可以使用Sequence直接过滤掉所有空值:

filterNotNull

并且val newFloats: List<Float> = floats .filterNotNull() .map { it + 1 } 仅保留map函数不返回mapNotNull的值,这也消除了null

filterNotNull

另一个我可以想到的结构可以与您的示例一起使用,尽管它非常难以理解并且非常复杂。但是这里是:

val newFloats: List<Float> = floats
    .mapNotNull { it?.plus(1) }

答案 1 :(得分:2)

如果您只想排除ArrayList中的空值,请调用mapNotNull而不是map。有关详细信息,请参见here

然后,您还需要处理value在lambda中为空的情况,您可以使用?.let进行如下操作:

localHistory.mapNotNull { value -> value?.let { decoration.decoratedValue(ref.format, it) } }

答案 2 :(得分:0)

再一次使用mapNotNull:

val strHistory = localHistory.mapNotNull { it ?: return@mapNotNull null
    decoration.decoratedValue(ref.format, it) }