在选项的scala模式匹配中,可能存在空指针解引用

gupuwyp2  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(394)

sonarqube报告显示了一个严重的错误
“此处取消引用空指针。执行代码时,这将导致nullpointerexception。”
对于下面的scala代码块

val someList = Option(listCommingFromJava.asScala.toList)
someList match {
  case Some(list) =>
        logger.info("List found: {}", list.map(someTransformFunction)) //Sonar Complains here of "Null pointer dereference of ?"
  case None => 
        logger.info("NoListFound")
}

记录器肯定已初始化。我可以共享声纳配置,但我看不出这段代码如何导致空指针解引用?我自己的结论是声纳在这种情况下是错误的。

pbwdgjma

pbwdgjma1#

在iterablescalaiterableconverter中,我们可以看到它参照iterablescalaiterable的结果创建asscala对象 null 如果java Iterable 参考是 null . 所以你的 listCommingFromJava 不应该 null . 否则, listCommingFromJava.asScala 会回来的 null 以及 listCommingFromJava.asScala.toList 将抛出 NullPointerException .

相关问题