Tobias Barth
Tobias Barth

Reputation: 76

Sonarqube does not complain about null Boolean unboxing

My Sonar 6.7.1 LTS instance does not recognize the NullPointerException in the following code:

public static boolean getBooleanFromMap() {
    final Map<String, Boolean> map = new HashMap<>();
    map.put("a", false);
    return map.get("b"); // line 118
}

public static void main(final String[] args) {
    System.out.println(getBooleanFromMap());
}

In Method "getBooleanFromMap()", a null Boolean value is retrieved from a HashMap, and that null value is then unboxed and returned as a boolean (line 118 in my example). The unboxing results in a NullPointerException, for example when the "main" method is run.

Sonar (with the built-in Java analyzer 5.1 (build 13090)) does not complain about that. Why?

Also, when I do not have a HashMap but a more complex method with a "org.apache.sling.api.resource.ValueMap", where the "get" method is even annotated with @CheckForNull, Sonar is also not seeing anything. Is there a way to make Sonar complain about that?

Upvotes: 2

Views: 394

Answers (1)

benzonico
benzonico

Reputation: 10833

The detection of unboxing of null values is currently not supported.

You can follow this ticket https://jira.sonarsource.com/browse/SONARJAVA-2126

Upvotes: 1

Related Questions