I have following code fragment:
List<SomeObject> allObjects = getNotNullCollection(SomeObject.class, getExpression());
Enumeration2Child mightBeNull = getEnumeration2();
try {
for (SomeObject someObject : allObjects) {
if (someObject == null) {
LOGGER.warn("Null Object " + getId() + " expression : " + getExpression().toString());
StringBuilder errorMsg = new StringBuilder();
for (Object element : allObjects) {
errorMsg.append(element != null ? element.toString() : "Null").append(" - ");
}
LOGGER.warn("allObjects: ", errorMsg.toString());
return false;
}
if (someObject.getValue() != null && someObject.getValue().equals(Enumeration1.VALUE) && !(Enumeration2.VALUE2.equals(mightBeNull) || Enumeration2.VALUE3.equals(mightBeNull))) {
return false;
}
}
} catch (NullPointerException e) {
}
And there is an NPE thrown in line:
if (someObject.getValue() != null && someObject.getValue().equals(Enumeration1.VALUE) && !(Enumeration2.VALUE2.equals(mightBeNull) || Enumeration2.VALUE3.equals(mightBeNull)))
As you can see above I check if someObject is null, i also check if someObject.getValue() is null so I should be safe in this part. And the rest of the if statements are just equals calls (and then I assume i would get NPE in equals method and not in if statement). What else might be wrong that I miss here? Thank you
EDIT: Hello, to give us closure on the issue. You were indeed right I didn't notice a crucial info but it was hard to investigate initially. I have added additional logging and found out there are 3 threads executing the same code. Now, this would not be a problem but due to implementation of DB caching getExpression() that returned allObjects , was shared between those threads. When one was checking against null values in this fragment: 'if (someObject.getValue() != null && someObject.getValue().' first check returned not Null and then another thread erased the value, hence the NPE was thrown by someObject.getValue(). So basically a concurrency issue