# Sonarqube does not detect null check by a method and showing possible NullPointerException

**URL:** <https://community.sonarsource.com/t/sonarqube-does-not-detect-null-check-by-a-method-and-showing-possible-nullpointerexception/28809>\
**Category:** Report False-positive / False-negative...\
**Tags:** sonarqube, java\
**Created:** [July 29, 2020, 10:29am UTC](https://community.sonarsource.com/t/sonarqube-does-not-detect-null-check-by-a-method-and-showing-possible-nullpointerexception/28809 "2020-07-29T10:29:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gayanvir](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/gayanvir/32/9955_2.png) [@Gayanvir](https://community.sonarsource.com/u/Gayanvir)\
**Post date:** [July 29, 2020, 10:29am UTC](https://community.sonarsource.com/t/sonarqube-does-not-detect-null-check-by-a-method-and-showing-possible-nullpointerexception/28809/1 "2020-07-29T10:29:27Z")

</div>

I have a gradle(6.1) project And I am using SonarQube 8.2. In the project, I have the following method.

```
public void updateCatalog(final Catalog catalog){
    if(Validator.validate(catalog)){
        addItem(catalog.getMainItem());
        ....
    }
}

```

the _catalog_ object is a nullable object and in the Validator.validate() method it has null check. so NullPointerException cannot be thrown from _catalog.getMainItem()_. Also please note that Validator class is from a third-party dependency (managed by us).

But the issue is SonarQube raise an issue saying

> “NullPointerException” will be thrown when invoking method “updateCatalog()”

But it cannot throw a NullPointerException. Seems like a false-positive. I like to know what might be the issue for this and how to resolve this.

---

<div class="post-metadata">

**Author:** ![Michael](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/michael/32/43_2.png) [@Michael](https://community.sonarsource.com/u/Michael)\
**Post date:** [August 19, 2020, 3:15pm UTC](https://community.sonarsource.com/t/sonarqube-does-not-detect-null-check-by-a-method-and-showing-possible-nullpointerexception/28809/3 "2020-08-19T15:15:21Z")

</div>

Hello @Gayanvir,

Thanks for reporting the issue. What you are observing here is unfortunately the result of a limitation of our Symbolic Execution (SE) engine.

As of today, the SE engine can not predict what would be the behavior of methods, if they are not part of the current file being analyzed (it also requires these methods to be not _overrideable_).

It has for consequences to lead to such exact FPs: the engine doesn’t learn that to return `true`, it requires `catalog` to be `not_null`. If the definition of `Validator.validate(...)` would have been part of the file, the engine would have been able to deduce this method behavior and behave correctly, not reporting the issue at the end.

As of today, we have no solution to workaround this issue, other than **marking the issue as an FP** on SonarQube/SonarCloud. We are also thinking about a third approach, which would allow end-users to define their own behaviors for their utility methods ([SONARJAVA-3502](https://jira.sonarsource.com/browse/SONARJAVA-3502)), but it’s still a draft.

Cheers,  
Michael

---

<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:** [August 26, 2020, 3:15pm UTC](https://community.sonarsource.com/t/sonarqube-does-not-detect-null-check-by-a-method-and-showing-possible-nullpointerexception/28809/5 "2020-08-26T15:15:31Z")

</div>

Closed on August 26, 2020, 3:15pm UTC

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