代碼審查工具可能的空指針引用抱怨safeScanWarnings的可能的空指針在saveSafeScan(...)在生產線如果(safeScanWarnings!= NULL & safeScanWarnings.size()> 0 )解釋並修復的
我在想這怎麼可能?這是因爲我們通過參考返回集合嗎?
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++;
}
}
}
雖然在您的示例代碼是在錯誤正確,如果有一個'&'和沒有'&&'。所以確保在真正的代碼中它是短路評估。如果情況已經如此,這是該工具中的一個錯誤。 – Voo
注意你的代碼示例上面的註釋是'if(safeScanWarnings!= null&safeScanWarnings.size()> 0)'這是錯誤的(錯誤的) – MeBigFatGuy