JDK14 Switch Expressions

Versions

  • SonarQube 8.5.1
  • JDK15
  • Gradle-wrapper 6.8.1
  • SonarQube-plugin 3.1.1

Error
I get an NPE when SonarQube is trying to scan a class with the new JDK14 switch expression.

./gradlew sonarqube -Dsonar.projectName=... -Dsonar.projectKey=... -Dsonar.links.homepage=... -Dsonar.links.ci=... -Dsonar.projectVersion=...

Unable to run check class org.sonar.java.se.SymbolicExecutionVisitor -  on file 'src/main/java/com/test/JDK14SwitchExpression.java', To help improve the SonarSource Java Analyzer, please report this problem to SonarSource: see https://community.sonarsource.com/

java.lang.NullPointerException: null
	at java.base/java.util.Objects.requireNonNull(Objects.java:208)
	at org.sonar.java.collections.AVLTree.get(AVLTree.java:76)
	at org.sonar.java.se.ProgramState.removeConstraintsOnDomain(ProgramState.java:320)
	at org.sonar.java.se.checks.StreamConsumedCheck.checkPostStatement(StreamConsumedCheck.java:212)
	at org.sonar.java.se.CheckerDispatcher.executePost(CheckerDispatcher.java:106)
	at org.sonar.java.se.CheckerDispatcher.addTransition(CheckerDispatcher.java:97)

MVE

public class JDK14SwitchExpression {
    public enum Day {
        MONDAY,
        TUESDAY,
        WEDNESDAY,
        THURSDAY,
        FRIDAY,
        SATURDAY,
        SUNDAY;
    }

    public String switchExpression(Day day) {
        return switch (day) {
            case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> "Weekday";
            case SATURDAY, SUNDAY -> "Weekend";
        };
    }
}

Is the java-version not set properly, or is this something else?

Hello @vehagn

This is a known problem on our side related to switch expressions without default (see SONARJAVA-3541).

The good news is that the fix is already available starting from SonarQube 8.6.

Best,
Quentin

1 Like

Thank you for the reply, I’ll just be patient then :slight_smile:

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