1
votes

I have below piece of code:

          if (null != result[1]) {
                mySearchTO.setDt(dateFormat2.parse(result[1].toString()));
            } else {
                mySearchTO.setDt(null);
            }

Where result is a object array and dateFormat2 is a SimpleDateFormat.

Findbugs is giving the following:

A possibly-null value is passed at a call site where all known target methods will unconditionally dereference it. This is very likely to result in a null pointer exception.

What does this message tell ? How to solve this error ?

I am checking the result for null so that my Parse method does not break. But if the result is null, I want to set the Dt field in SearchTO to some default which here I am taking as null.

Is there a better default option for date ?

Thanks for reading!

4
Is the error in dataFormat2.parse or mySearchTO.setDt? - Gabe
@Gabe: Its in mySearchTO.setDt - Vicky

4 Answers

2
votes

It could be complaining that mySearchTO or result could be null.

0
votes

Is it possible that your mySearchTO field itself may be null?

Otherwise, it may not be realizing that result[1] is already being checked for null - possibly complicated by the fact that it's reading a value out of an array. What if you do something like Object o = result[1]; above the conditional, check for o != null as the conditional, and then call o.toString() within the conditional?

0
votes

The message refer to array result, you only check that element under index 1 is not null, but array itself could be also null.

 if (result != null && null != result[1]) {
    mySearchTO.setDt(dateFormat2.parse(result[1].toString()));
 } else {
    mySearchTO.setDt(null);
 }

I assumed that mySearchTO and dataForma2 are initialized correctly.

0
votes

Per my reading of NP_NULL_PARAM_DEREF explanation, Findbugs is trying to warn you that this statement will bite you with NPE:

            mySearchTO.setDt(null);
            // guess there's line at mySearchTO.setDt looking like...
            // ... valuePassedToDt.toString() - would give NPE for null

...This method call passes a null value for a nonnull method parameter. Either the parameter is annotated as a parameter that should always be nonnull, or analysis has shown that it will always be dereferenced...

To test if that's indeed the case, replace above with something like

            throw new RuntimeException("can't handle null in result[1]");

then re-run Findbugs and see if the warning disappears.