# False positive "possible null" on Java code

**URL:** <https://community.sonarsource.com/t/false-positive-possible-null-on-java-code/47582>\
**Category:** Eclipse\
**Tags:** false-positive, eclipse, sonarqube-ide, java\
**Created:** [August 4, 2021, 6:00pm UTC](https://community.sonarsource.com/t/false-positive-possible-null-on-java-code/47582 "2021-08-04T18:00:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![H.Lo](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/h.lo/32/24616_2.png) [@H.Lo](https://community.sonarsource.com/u/H.Lo)\
**Post date:** [August 4, 2021, 6:00pm UTC](https://community.sonarsource.com/t/false-positive-possible-null-on-java-code/47582/1 "2021-08-04T18:00:35Z")

</div>

Hi!  
I have pretty simple code that makes SonarLint report false-positive on `city.getCountry()`:

```
if (address.getCity() != null) {
    final City city = address.getCity();
    // ...
    keywords.addAll( getKeywords( city.getCountry() ) );
    // ...
}
```

The warning is: **A “NullPointerException” could be thrown; “city” is nullable here.**

The idea is to avoid creating City instance if none is available, so first goes the check, and if city exists, an instance is created which is obviously not null because of previous check.

**OS** : Windows 10  
**IDE:** Eclipse STS 4.11.0.RELEASE  
**SonarLint:** 6.0.0.34844  
**Language:** Java 8

---

<div class="post-metadata">

**Author:** ![H.Lo](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/h.lo/32/24616_2.png) [@H.Lo](https://community.sonarsource.com/u/H.Lo)\
**Post date:** [August 4, 2021, 6:01pm UTC](https://community.sonarsource.com/t/false-positive-possible-null-on-java-code/47582/2 "2021-08-04T18:01:12Z")

</div>

![sonar-linter-bug](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/0/0/00f1fc256993beec631d2a6fddee7218b8919d74.png)

---

<div class="post-metadata">

**Author:** ![JBL\_SonarSource](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/jbl_sonarsource/32/64_2.png) [@JBL\_SonarSource](https://community.sonarsource.com/u/JBL_SonarSource)\
**Post date:** [August 4, 2021, 7:18pm UTC](https://community.sonarsource.com/t/false-positive-possible-null-on-java-code/47582/3 "2021-08-04T19:18:33Z")

</div>

Hello, welcome to the community! And thank you for reporting this behavior.

The problem is, the analyzer has no guarantee that the 2 subsequent calls to `getCity()` will return the same thing. Even assuming that `address` is a POJO and `getCity` a simple getter method, the internal state of `address` could change between the 2 calls - the static analyzer cannot prove that the second call does not return `null`.

One way to write it with statically correct null checks would be:

```java
final City city = address.getCity();
if (city != null) {
    // ...
    keywords.addAll( getKeywords( city.getCountry() ) );
    // ...
}

```

Note that there is no instance creation here, only an assignment to a variable in the local scope that points to the instance returned by `getCity`.

---

<div class="post-metadata">

**Author:** ![H.Lo](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/h.lo/32/24616_2.png) [@H.Lo](https://community.sonarsource.com/u/H.Lo)\
**Post date:** [August 5, 2021, 8:32am UTC](https://community.sonarsource.com/t/false-positive-possible-null-on-java-code/47582/4 "2021-08-05T08:32:34Z")

</div>

As this code snippet is a part of indexing engine for a whole lot of data, the idea was to have only one null check instead of assignment and null check. But it can go this way too, I agree.

---

<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 12, 2021, 8:33am UTC](https://community.sonarsource.com/t/false-positive-possible-null-on-java-code/47582/5 "2021-08-12T08:33:02Z")

</div>

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