次のコードを使用すると、NP_NULL_PARAM_DEREF: メソッド呼び出しで null 以外のパラメーターに 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 を取得したのですか?どのような変更がより良いでしょうか? これは、パラメーターを final と宣言したために発生したのでしょうか? または、NullPointerException をキャッチしていないためですか? 私はそれを捕まえたくありません、それはどこかで捕まえられるべきです。たぶん、calledAnywhereIDoNotCare() で NullPointerException のスローを宣言する必要がありますか?
ご協力ありがとうございました。タール