Hi, I have some code which reportedly has NP_GUARANTEED_DEREF problem from Findbugs. Now, looking at my code, I do not quite understand what is wrong with it, can anyone suggest what the problem is.
public void test() {
String var = "";
int index = 2;
if (index == -1) {
var = String.class.getName();
if (var.length() == 0) {
var = null;
}
} else {
var = Integer.class.getName();
if (var.length() == 0) {
var = null;
}
}
if (var == null) {
throw new NullPointerException("NULL");
}
}
Now, checking the error in Findbugs, he identifies two assignments var = null;as the cause of the error, but I do not quite understand why. It doesn't look like I'm really doing something with an object var, I'm just doing a Null check. An example is taken from real production code, but is devoid of everything that is not required to reproduce the error. What interests me is if this is a false positive or not. And if not what would be a suitable solution.
Findbugs: http://findbugs.sourceforge.net/bugDescriptions.html#NP_GUARANTEED_DEREF
[UPDATE] Findgugs Bugtracker Sourceforge, https://sourceforge.net/tracker/?func=detail&aid=3277814&group_id=96405&atid=614693
.