# SonarQube - Null Pointer Dereference Issue

**URL:** <https://community.sonarsource.com/t/sonarqube-null-pointer-dereference-issue/235>\
**Category:** Report False-positive / False-negative...\
**Tags:** java\
**Created:** [June 14, 2018, 1:18pm UTC](https://community.sonarsource.com/t/sonarqube-null-pointer-dereference-issue/235 "2018-06-14T13:18:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonsirak](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@simonsirak](https://community.sonarsource.com/u/simonsirak)\
**Post date:** [June 14, 2018, 1:18pm UTC](https://community.sonarsource.com/t/sonarqube-null-pointer-dereference-issue/235/1 "2018-06-14T13:18:46Z")

</div>

Hi!

I was fixing some issues gathered by SonarQube when I stumbled upon the following issue:

“SonarQube violation: Possible null pointer dereference in \_\_\_ due to return value of called method”

This error was found in the following code: `... else if (foo.list().length > 0) { ... }`

I attempted to resolve this by rewriting as: `... else if (null != foo.list() && foo.list().length > 0) { ... }`

`foo` is an instance of the File class in Java, and is directly instantiated through `new File(...)`

The only potential null is the return value of list(). That is why I explicitly check for this in my attempted solution.

My question is; is my solution incorrect? If not, this might be a bug.

I’m currently using SonarQube version 5.6.6.

Best Regards,  
Simon Sirak

---

<div class="post-metadata">

**Author:** ![saberduck](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/saberduck/32/27_2.png) [@saberduck](https://community.sonarsource.com/u/saberduck)\
**Post date:** [June 14, 2018, 1:25pm UTC](https://community.sonarsource.com/t/sonarqube-null-pointer-dereference-issue/235/2 "2018-06-14T13:25:51Z")

</div>

Hello,

better way to handle this is to store the result of method invocation in a variable. There is no guarantee that subsequent invocations are returning the same value, that’s why issue is raised.

---

<div class="post-metadata">

**Author:** ![simonsirak](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@simonsirak](https://community.sonarsource.com/u/simonsirak)\
**Post date:** [June 14, 2018, 2:06pm UTC](https://community.sonarsource.com/t/sonarqube-null-pointer-dereference-issue/235/3 "2018-06-14T14:06:23Z")

</div>

Yes, that worked! Thank you so much!!

---

<div class="post-metadata">

**Author:** ![NicoB](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/nicob/32/3_2.png) [@NicoB](https://community.sonarsource.com/u/NicoB)\
**Post date:** [June 14, 2018, 2:20pm UTC](https://community.sonarsource.com/t/sonarqube-null-pointer-dereference-issue/235/4 "2018-06-14T14:20:03Z")

</div>

Hi there,

Quick note on this:

> [@simonsirak](#):
>
> I’m currently using SonarQube version 5.6.6.

This likely means that you’re on an ancient version of the Java analyzer. There’d be huge value in jumping onto the latest version of the ecosystem (notably SonarQube), and benefit from all latest analyzers (e.g. [the Java one](https://docs.sonarqube.org/display/PLUG/SonarJava)). (in the meantime, in case of any other false-positive suspicion, make sure to check those latest versions as bunch of improvements are regularly released)

---

<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:** [April 8, 2021, 9:37am UTC](https://community.sonarsource.com/t/sonarqube-null-pointer-dereference-issue/235/5 "2021-04-08T09:37:59Z")

</div>

Closed on April 8, 2021, 9:37am UTC

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