"Call "Optional#isPresent()" before accessing the value" false-positive with ternary operator

Sonarqube information:

  • SonarQube version 8.9.2 (build 46101), using Docker image tag “8.9-community”.
  • Language: Java.

This line reports ‘Call “Optional#isPresent()” before accessing the value.’:
String accountId = hostUser.getUserAccountId().isPresent() ? hostUser.getUserAccountId().get() : null;

Hello @URE and welcome to the community.

You can have a look at this thread: [False Positive]A "NullPointerException" could be thrown;

The code is slightly different and it’s another rule, but the reasoning is the same.


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