Comparison between a Wrapper class and and its corresponding primitive type can produce a NullPointerException

Comparison between a Wrapper class and and its corresponding primitive type can produce a NullPointerException

// Noncompliant Code Example
Long wrapper = null;
long baseType = 100;

// Unboxing of ‘wrapper’ may produce ‘NullPointerException’
if (wrapper == baseType) {

}

// Compliant Code Example
Long wrapper = null;
long baseType = 100;

if (wrapper == Long.valueOf(baseType)) {

}
Don’t hesitate to share rule ideas. Even when they don’t match our guidelines it might make somebody else think of an alternative rule.

Hey @Anicet_Didi ,

Thanks for the idea, and the good news is that we have a ticket for this: SONARJAVA-2126
It has been in our backlog for a long time, and we will see if we can address it some times in the future.

Cheers,
Michael

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.