# Not validating Objects.nonNull

**URL:** <https://community.sonarsource.com/t/not-validating-objects-nonnull/12942>\
**Category:** Report False-positive / False-negative...\
**Tags:** java\
**Created:** [August 9, 2019, 6:39am UTC](https://community.sonarsource.com/t/not-validating-objects-nonnull/12942 "2019-08-09T06:39:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepak\_Gusain](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/deepak_gusain/32/5552_2.png) [@Deepak\_Gusain](https://community.sonarsource.com/u/Deepak_Gusain)\
**Post date:** [August 9, 2019, 6:39am UTC](https://community.sonarsource.com/t/not-validating-objects-nonnull/12942/1 "2019-08-09T06:39:10Z")

</div>

SonarQube Version 6.7.1  
Java 1.8  
For below Code sonar raise bug:  
A “NullPointerException” could be thrown; “testObject” is nullable here.  
But we are checking non null condition in if clause.

```auto
if (Objects.nonNull(testObject)) {
			testObject.getId();
}

```

If we change if clause

```auto
if(testObject != null)

```

then sonarqube will not show this issue.

---

<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:** [August 9, 2019, 9:49am UTC](https://community.sonarsource.com/t/not-validating-objects-nonnull/12942/2 "2019-08-09T09:49:24Z")

</div>

Hello @Deepak_Gusain,

I did not manage to reproduce your false positive with the [current version](https://github.com/SonarSource/sonar-java) of SonarJava.

Since your version is probably quite old ([this test](https://github.com/SonarSource/sonar-java/blob/d3eb9295131104fcda5871f919fd905534b15417/java-frontend/src/test/files/se/ObjectsMethodsTest.java#L21) is 4 years old!), you should consider updating SonarJava!

Best,  
Quentin

---

<div class="post-metadata">

**Author:** ![quonn77](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/quonn77/32/6937_2.png) [@quonn77](https://community.sonarsource.com/u/quonn77)\
**Post date:** [October 11, 2019, 12:52pm UTC](https://community.sonarsource.com/t/not-validating-objects-nonnull/12942/3 "2019-10-11T12:52:31Z")

</div>

Hi this happens also with the latest version of SonarQube 7.9.1

---

<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:** [November 27, 2019, 9:06am UTC](https://community.sonarsource.com/t/not-validating-objects-nonnull/12942/4 "2019-11-27T09:06:41Z")

</div>

In order to help you, can you provide a complete reproducer causing the issue?  
I still don’t see any wrong behavior with the example given.

---

<div class="post-metadata">

**Author:** ![gebitang](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/gebitang/32/16117_2.png) [@gebitang](https://community.sonarsource.com/u/gebitang)\
**Post date:** [June 16, 2020, 7:11am UTC](https://community.sonarsource.com/t/not-validating-objects-nonnull/12942/5 "2020-06-16T07:11:52Z")

</div>

here with the same problem. And Env info as following:

```auto
# scanner
INFO: SonarQube Scanner 4.2.0.1873
INFO: Java 1.8.0_231 Oracle Corporation (64-bit)
INFO: Linux 4.19.2-1.el7.elrepo.x86_64 amd64

#sonarQube:
Community EditionVersion 7.6 (build 21501)

```

the complaint one:

1. Implies ‘executionChain’ is null.
2. Exception is thrown.
3. ‘Throwable’ is caught.
4. ‘executionChain’ is dereferenced.

 ![complaint](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/2X/c/ccf0fa296fcce6a869b50743695ffe9805446d9e.png)

If use `executionChain != null` method, there isn’t any complaint anymore.

 ![NoComplatin](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/2X/6/65cdb26fb3244fa0d119fd2ba1cd8dba96548304.png)

this is the demo code snippets:

```auto
public Object doServiceImplPointAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Throwable t = null;

        Object object = null;

        Object[] paramValues = null;

        MethodSignature methodSignature = null;

        AdviceHandlerExecutionChain executionChain = null;
        try {
            object = joinPoint.getTarget();
            methodSignature = (MethodSignature) joinPoint.getSignature();
            Method method = methodSignature.getMethod();
            paramValues = joinPoint.getArgs();
            RequestAdvice requestAdvice = method.getAnnotation(RequestAdvice.class);

            executionChain = buildAdviceHandlerExecutionChain(requestAdvice);

            boolean res = executionChain.applyPreHandle(object, paramValues);
            if (res) {
                Object result = joinPoint.proceed();
                executionChain.applyPostHandle(joinPoint.getTarget(), paramValues);
                return result;
            }
            return null;
        } catch (Throwable t1) {
            t = t1;
            throw t1;
        } finally {
            if (executionChain != null) {
                executionChain.triggerAfterCompletion(object, paramValues, t);
            }
        }
    }

```

---

<div class="post-metadata">

**Author:** ![gebitang](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/gebitang/32/16117_2.png) [@gebitang](https://community.sonarsource.com/u/gebitang)\
**Post date:** [June 16, 2020, 7:20am UTC](https://community.sonarsource.com/t/not-validating-objects-nonnull/12942/6 "2020-06-16T07:20:11Z")

</div>

with the same environment, the following code snippets didn’t raise any complaint.

```auto
public class NonNull {
    public void testObjectsMethods(Object b) {
        if(Objects.nonNull(b)) {
            b.toString();
        }else {
            System.out.println("b is null");
        }
    }
    public void testObjectsMethod2(Object b) {
        if(b != null) {
            b.toString();
        }else {
            System.out.println("b is null");
        }
    }
    public Object testWithTryCatch(Object o) throws Exception{
        Object some = null;
        try {
            if(Objects.nonNull(o)) {
                some = o;
            }
        }catch (Exception e) {
            throw e;
        }finally {
            if(Objects.nonNull(some)) {
                some.toString();
            }
        }
        return some;
    }
}

```

Why the first code snippets raise a complaint?

---

<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:** [June 17, 2020, 3:13pm UTC](https://community.sonarsource.com/t/not-validating-objects-nonnull/12942/8 "2020-06-17T15:13:46Z")

</div>

Hello,  
Thanks for taking the time to report this issue and provide a reproducer.  
However, I’m afraid I don’t see any quick explanation to your problem, the answer is the same as the one from the original post, SonarQube 7.6 is already past EOL. You might try upgrading to 7.9.3, the current LTS, or 8.3, the most recent version.  
The Java analyzer has been heavily changed between 7.6 and 8.3, and could definitely resolve the problem you are facing here.

Best,  
Quentin
