gpt4 book ai didi

java - 发条警告 : Null pointer dereference of where null is returned from a method

转载 作者:行者123 更新时间:2023-11-30 07:54:22 26 4
gpt4 key购买 nike

在使用 klocwork 进行错误分析时,我收到警告“nextLineDn”的空指针取消引用,其中从方法返回 null

显然其他静态分析工具findbug也给出了相同的警告。

但显然我在使用它之前检查是否为空/空。

      int noOfLines = device.getLines().size();     
if( lineNo != 0 && noOfLines > lineNo ) // if next line exists
{
nextLineDn = device.getDn(lineNo+1);
if(!Util.isNullOrEmpty(nextLineDn))
{
return (nextLineDn.contains("@")) ? nextLineDn.split("@")[0] : nextLineDn;
}
}

类Util:

public static boolean isNullOrEmpty(String str) {
return (str == null || str.isEmpty());
}

有人可以给我一些关于这个的想法吗?我在相同条件下收到这么多警告。不知道还能做什么来删除警告。

最佳答案

由于 Klocwork Insight 是一个静态源代码分析工具,它可能无法进一步破译您在 Util 类中有一个名为 isNullOrEmpty() 的方法,其中您实际上正在执行 null 检查。因此,它在您的 IDE 中显示警告。

静态分析工具试图提前发现潜在的缺陷。因此,Klocwork 会告诉您:device.getDn() 可能返回 null,请小心使用 nextLineDn

但是,如果您输入像 (nextLineDn!=null) 这样的代码,我想它不会在那里标记警告。 (尝试让我们知道)

关于java - 发条警告 : Null pointer dereference of where null is returned from a method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32880036/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com