Hi all.
I have a interesting code block and an error from SonarLint -
Bug, Major, java:S2259: Null pointers should not be dereferenced.
And here is a method:
public DataType extract(final CloudEvent cloudEvent) {
if (cloudEvent == null) {
LOG.warn("Can't process message because message is null");
return null;
}
if (cloudEvent.getData() == null) {
LOG.warn("Can't process message because data part is not set.");
return null;
}
if (!(cloudEvent.getData() instanceof JsonCloudEventData)) {
LOG.warn("Can't process message because message format is not correct. "
+ "JsonCloudEventData expected and {} received.",
cloudEvent.getData().getClass().getName());
return null;
}
// Rest of logic
}
SonarLint shows the issue here - cloudEvent.getData().getClass().
Method getData() has @Nullable annotation. And, as you can see, there are plenty of checks for null value.
Interesting is when even I rewrite the issued part like this -
cloudEvent.getData() != null
? cloudEvent.getData().getClass().getName()
: "Empty Message"
it still shows the bug in here.
Could somebody please explain why null-pointer rule is ignoring all previous checks?
Thanks in advance.