Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to fix the Findbugs issue "Null value is guaranteed to be dereferenced" NP_GUARANTEED_DEREF
    text
    copied!<p>Hi I have got some code that is reported as having the NP_GUARANTEED_DEREF issue by Findbugs. Now looking at my code I don't quite understand what is wrong with it, can anyone suggest what the problem is. </p> <pre><code>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"); } } </code></pre> <p>Now drilling into the Error in Findbugs it highlights the two assignments to <code>var = null;</code> as cause for the bug but I don't quite understand why. It is not like I am actually doing anything with the <code>var</code> object I am just doing a Null check. The example is taken from real production code but stripped of anything that wasn't needed to reproduce the error. What I am wondering if this is a false positive or not. And if not what would be an appropriate fix.</p> <p>Here is the link to the Findbugs Bug Detail: <a href="http://findbugs.sourceforge.net/bugDescriptions.html#NP_GUARANTEED_DEREF" rel="nofollow">http://findbugs.sourceforge.net/bugDescriptions.html#NP_GUARANTEED_DEREF</a></p> <p>[UPDATE] After recieving some feedback on this issue I have now logged this as a False Positive in the Findbugs Bugtracker on Sourceforge the link is <a href="https://sourceforge.net/tracker/?func=detail&amp;aid=3277814&amp;group_id=96405&amp;atid=614693" rel="nofollow">https://sourceforge.net/tracker/?func=detail&amp;aid=3277814&amp;group_id=96405&amp;atid=614693</a></p> <p>Conversation about the problem will continue there.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload