Explanation and correction for possible dereferencing of a null pointer

The code viewer complains about the Possible reversal of the safeScanWarnings null pointer in saveSafeScan (...) in the if line (safeScanWarnings! = Null and safeScanWarnings.size ()> 0)

I wonder how is this possible? Is it because we are returning the collection by reference?

protected void saveSafeScan(final Response response, final Dtec dtec) throws dtecException { Collection<String> safeScanWarnings = dtec.getSafeScanWarnings(); if (safeScanWarnings!=null && safeScanWarnings.size()>0) { Iterator<String> iterator = safeScanWarnings.iterator(); int i = 0; while (iterator.hasNext()) { String safeScanCode = iterator.next(); if (i == 0) { response.setSafeScanCode(safeScanCode); response.setSafeScanCodeText(getMessage(String.format("DTECRESPONSE_SAFESCANCODE_%s", StringUtils.trimToEmpty(safeScanCode)))); } SafeScanWarning safeScan = new SafeScanWarning(); safeScan.setCode(safeScanCode); safeScan.setMessage(String.format("DTECRESPONSE_SAFESCANCODE_%s", StringUtils.trimToEmpty(safeScanCode))); safeScan.setPriority(i); response.getSafeScanWarnings().add(safeScan); i++; } } } 
+6
source share
3 answers

If it really points to this line, it looks like an error in the code viewer for me.

As a local variable, there is no chance that it will be changed by anything else between validation and calling size() - so that it will not cause it to throw a NullPointerException .

+10
source

There is a statement branch that, if executed, ensures that null is dereferenced, which will cause a NullPointerException to be NullPointerException when the code is executed. Of course, the problem may be that the branch or statement is not valid and that a NullPointerException cannot be thrown. The decision that it is not possible FindBugs .

0
source

The "dtec" attribute must be secured:

  if (null!=dtec && null!=safeScanWarnings && safeScanWarnings.size()>0) { Collection<String> safeScanWarnings = dtec.getSafeScanWarnings(); 
0
source

Source: https://habr.com/ru/post/893718/


All Articles