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

**URL:** <https://community.sonarsource.com/t/call-optional-ispresent-before-accessing-the-value-false-positive-with-ternary-operator/49937>\
**Category:** Report False-positive / False-negative...\
**Tags:** java\
**Created:** [September 17, 2021, 7:45am UTC](https://community.sonarsource.com/t/call-optional-ispresent-before-accessing-the-value-false-positive-with-ternary-operator/49937 "2021-09-17T07:45:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![URE](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ure/32/25769_2.png) [@URE](https://community.sonarsource.com/u/URE)\
**Post date:** [September 17, 2021, 7:45am UTC](https://community.sonarsource.com/t/call-optional-ispresent-before-accessing-the-value-false-positive-with-ternary-operator/49937/1 "2021-09-17T07:45:10Z")

</div>

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;`

---

<div class="post-metadata">

**Author:** ![Quentin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/quentin/32/74716_2.png) [@Quentin](https://community.sonarsource.com/u/Quentin)\
**Post date:** [September 23, 2021, 9:29am UTC](https://community.sonarsource.com/t/call-optional-ispresent-before-accessing-the-value-false-positive-with-ternary-operator/49937/2 "2021-09-23T09:29:33Z")

</div>

Hello @URE and welcome to the community.

You can have a look at this thread: [[False Positive]A "NullPointerException" could be thrown;](https://community.sonarsource.com/t/false-positive-a-nullpointerexception-could-be-thrown/47025)

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

Best,  
Quentin

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [September 30, 2021, 9:30am UTC](https://community.sonarsource.com/t/call-optional-ispresent-before-accessing-the-value-false-positive-with-ternary-operator/49937/3 "2021-09-30T09:30:25Z")

</div>

Closed on September 30, 2021, 9:30am UTC

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