gpt4 book ai didi

java - FindBugs:为什么我得到 NP_NULL_PARAM_DEREF 以及什么会更好?

转载 作者:行者123 更新时间:2023-12-01 04:24:58 27 4
gpt4 key购买 nike

使用以下代码,我得到NP_NULL_PARAM_DEREF:方法调用为非空参数传递null:

public void calledAnywhereIDoNotCare() {
//[...]
//parameter could be null but shouldn't ever be by logic
method(parameter); //FindBugs says the problem is here
//[...]
}

public final ReturnType method(final ParameterType parameter) {
//this method do nothing but simply call anotherMethod()
return anotherMethod(parameter, false);
}

public final ReturnType anotherMethod(final ParameterType parameter, boolean boolParam) {
if (parameter == null) {
//just in case logic is wrong
throw new NullPointerException("I know it shouldn't be null by logic, but it is null!");
}
//do something very usefull
//[...]
}

所以,我的问题是:为什么我会得到这个 NP_NULL_PARAM_DEREF 以及哪些更改会更好?我得到这个是因为将参数声明为最终的吗?还是因为没有捕获 NullPointerException?我不想捕获它,它应该在外面的某个地方被捕获。也许我应该在 CalledAnywhereIDoNotCare() 中声明抛出 NullPointerException?

感谢您的帮助。塔尔莱

最佳答案

在您的评论中,您写道参数“可以”为空,但由于程序逻辑,永远不会为空。 FindBugs 不知道/理解逻辑。最好的方法是使用 edu.umd.cs.findbugs.annotations.SuppressWarnings 注释让 FindBugs 忽略此方法中的 NP_NULL_PARAM_DEREF。请参阅http://findbugs.sourceforge.net/manual/annotations.html

关于java - FindBugs:为什么我得到 NP_NULL_PARAM_DEREF 以及什么会更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18587851/

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