NPE on S4276: Optinal.ofNullable() causes analysis to fail

java

(Sergiu Pantiru) #1

Hello,

I’m having some errors running sonar on a piece of code:

 private Optional<Double> tryConvert(
            final double value,
            final Unit unit) {
        return Optional
                .ofNullable(conversionRules.get(unit))
                .map(f -> f.apply(value));
    }

Sonar fails with NullPointerException and from what I’ve see it’s because of this line: .ofNullable(conversionRules.get(unit))

I’m using JDK 11, sonar-maven-plugin:3.5.0.1254 and SonarQube Community Edition Version 7.4 (build 18908)

Thank you,
Sergiu


(Michael Gumowski) #2

Hello,

Thanks for the feedback. Unfortunately your code snippet is not self-contained and there is some unknown variables and Types.

In order to help me identify the issue:

  • Can you try to provide a code snippet which does not require any external dependency?
  • Can you provide the full log of the exception?
  • Can you check on your SonarQube what’s the version of the SonarJava analyzer?
    • Your issue seems similar to the one from the “NPE with lambdas in S4738” thread, which have been fixed in SonarJava 5.9.2, released a few days ago. Updating to latest version of SonarJava should fix the issue

Cheers,
Michael


(Sergiu Pantiru) #3

Full exception is: (I will collect the rest of the information and post it)

Caused by: org.sonar.java.AnalysisException: SonarQube is unable to analyze file : Measurement.java
at org.sonar.java.ast.JavaAstScanner.simpleScan(JavaAstScanner.java:105)
at org.sonar.java.ast.JavaAstScanner.scan(JavaAstScanner.java:68)
at org.sonar.java.JavaSquid.scanSources(JavaSquid.java:116)
at org.sonar.java.JavaSquid.scan(JavaSquid.java:110)
at org.sonar.plugins.java.JavaSquidSensor.execute(JavaSquidSensor.java:93)
at org.sonar.scanner.sensor.SensorWrapper.analyse(SensorWrapper.java:45)
at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:88)
at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:62)
at org.sonar.scanner.phases.AbstractPhaseExecutor.execute(AbstractPhaseExecutor.java:74)
at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:164)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:319)
at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:314)
at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:288)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
at org.sonar.scanner.task.ScanTask.execute(ScanTask.java:48)
at org.sonar.scanner.task.TaskContainer.doAfterStart(TaskContainer.java:82)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:136)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:122)
at org.sonar.scanner.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:131)
at org.sonar.batch.bootstrapper.Batch.doExecuteTask(Batch.java:116)
at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:71)
at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
at com.sun.proxy.$Proxy23.execute(Unknown Source)
at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:171)
at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:128)
at org.sonarsource.scanner.maven.bootstrap.ScannerBootstrapper.execute(ScannerBootstrapper.java:65)
... 23 more

Caused by: java.lang.NullPointerException
at org.sonar.java.AnalyzerMessage.textSpanBetween(AnalyzerMessage.java:134)
at org.sonar.java.AnalyzerMessage.textSpanFor(AnalyzerMessage.java:123)
at org.sonar.java.model.DefaultJavaFileScannerContext.createAnalyzerMessage(DefaultJavaFileScannerContext.java:163)
at org.sonar.java.model.DefaultJavaFileScannerContext.reportIssueWithFlow(DefaultJavaFileScannerContext.java:127)
at org.sonar.java.model.DefaultJavaFileScannerContext.reportIssue(DefaultJavaFileScannerContext.java:120)
at org.sonar.java.model.DefaultJavaFileScannerContext.reportIssue(DefaultJavaFileScannerContext.java:114)
at org.sonar.plugins.java.api.IssuableSubscriptionVisitor.reportIssue(IssuableSubscriptionVisitor.java:62)
at org.sonar.java.checks.SpecializedFunctionalInterfacesCheck.lambda$checkVariableTypeAndInitializer$3(SpecializedFunctionalInterfacesCheck.java:82)
at java.base/java.util.Optional.ifPresent(Optional.java:183)
at org.sonar.java.checks.SpecializedFunctionalInterfacesCheck.checkVariableTypeAndInitializer(SpecializedFunctionalInterfacesCheck.java:80)
at org.sonar.java.checks.SpecializedFunctionalInterfacesCheck.visitNode(SpecializedFunctionalInterfacesCheck.java:59)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.lambda$visit$7(VisitorsBridge.java:293)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:296)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:278)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:300)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:278)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:300)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:278)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:300)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:278)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:300)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:278)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:300)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:278)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:300)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:278)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:300)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:278)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:300)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visitChildren(VisitorsBridge.java:278)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.visit(VisitorsBridge.java:300)
at org.sonar.java.model.VisitorsBridge$ScannerRunner.run(VisitorsBridge.java:269)
at org.sonar.java.model.VisitorsBridge.visitFile(VisitorsBridge.java:140)
at org.sonar.java.ast.JavaAstScanner.simpleScan(JavaAstScanner.java:96)
... 56 more

(Michael Gumowski) #4

Thank you for the logs.

From what I can already see here, the issue is caused by rule squid:S4276, which tries to report on an non-existing node from the AST.

If the current failure is causing your analysis to fail and preventing you to work, I would suggest you to temporarily disable the rule. I’m however quite sure that SonarJava 5.9.2 should have fixed the issue already, as we had to deal with a similar problem in SONARJAVA-2967.

Now, if you are already using version 5.9.2 of SonarJava and still getting the issue, I would be very interested by a self-contained reproducer.

Regards,
Michael


(Sergiu Pantiru) #5

I’m using SonarJava * 5.9 (build 16391).
I will try to upgrade to 5.9.2 and let you know if it works or not.


(Sergiu Pantiru) #6

All works after upgrading to 5.9.2.

Thanks a lot for the quick support :slight_smile:!