Optional.ofNullable(null).orElse is reported as a potential NPE

  • versions used (SonarQube, Scanner, Plugin, and any relevant extension) : 7.9.2

I also tested it with Community Edition Version 9.3 and it reported the same problem

  • Java version : 11
  • error observed (wrap logs/code around triple quote) :

The following code is reported as a potential NPE when this cannot happen considering how the call works.

String myHeader = Optional.ofNullable(inputHeaders).orElse(MultiMap.caseInsensitiveMultiMap())

MultiMap.caseInsensitiveMultiMap() returns a HashMap<String,String> in case it matters

Hey @sonaruser1231,

Thanks for bringing up the issue. It indeed looks like this is a false positive.

Could you provide a self-contained code sample so that we can reproduce the issue?

@Dorian_Burihabwa Please see the steps to reproduce :

  1. Download and configure Sonar 9.3 : https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-

  2. Configure a new project

  3. Run the scan

mvn clean verify sonar:sonar -Dsonar.projectKey=nullable-problem -Dsonar.host.url=http://localhost:9000 -Dsonar.login=xx

See Maven project attached

nullable-problem.zip (3.1 KB)

Thank you, @sonaruser1231, I can reproduce the example with the sample you posted.

A ticket has been created to handle the issue.

1 Like

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