How to fix Findbugs problem "Null value guaranteed by dereferencing" NP_GUARANTEED_DEREF

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) {// FINBUGS reports on this line NP_GUARANTEED_DEREF
    /*
     * There is a statement or branch that if executed guarantees that a value
     * is null at this point, and that value that is guaranteed to be
     * dereferenced (except on forward paths involving runtime exceptions).
     */
    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

.

+5
4

. FB . . , throw new NullPointerException throw new RuntimeException, .

, , . , NPE. , NPE .

+5

FindBugs, . findbugs.sf.net

+3

, , FindBugs, - , , , . .

if (x == null) throw new NullPointerException()

, x. , , , , , null , .

, , .

, . , null var, , . , ?

+2

, :

, if , null , ( , )

, , , var null - findbugs , var if.

, , , , var .

, , :

if (null == var)

, , = 's/

0

All Articles