False fault 'Null pointers should not be dereferenced' using Optional

SonarLint IntelliJ 4.0.0.2916

Java Rule ‘Null pointers should not be dereferenced’

I’m getting this fault in the next code when obj can’t be null in any scenario. Seems the analizer don’t recognice the logic with the funcional object Optional.

final Obj obj = Optional.ofNullable(oldObj).orElse(new Obj());

if (obj.getSomeProperty() == null) {

Hi,
I struggle to understand where the issue is raised exactly, could you specify more precisely where the issue is raised (and so which object is assumed to be null) ? Is your sonarlint using connected mode ?

The real code is

    ...
    registrarEvento010Enviado(proceso, fraEnvio010, fechaGeneracion);  // The sonar fault is marked in this line.
    }
}

private void registrarEvento010Enviado(final Proceso proceso, final FraEnvio010 envio010,
                                       final LocalDateTime fecGeneracion) {

    final FraEnvio010 fraEnvio010 = Optional.ofNullable(envio010).orElse(new FraEnvio010());

    if (fraEnvio010.getFraEnvioPK() == null) { // This access cause the sonar fault. But, is impossible to produce a NullPointer
        fraEnvio010.setFraEnvioPK(new FraEnvioPK());
    }

...

// Alternative to Optional and without sonar fault:
//
// FraEnvio010 fraEnvio010;
//
// if (Objects.isNull(envio010)) {
// fraEnvio010 = new FraEnvio010();
// } else {
// fraEnvio010 = envio010;
// }

May be is better understable reading real code. At the end, sonar advise me that the use of this function will throw a null pointer. But, is imposible to happend.

I really struggle to understand the issue you are facing given how the rule is supposed to be working.

Could you share a small screenshot of the issue ? Are you using latest version of sonarlint ? if in connected mode what’s the version of sonarjava on your server ?

As you can see in the screenshot. Sonar warn for a possible null pointer. But is impossible to be throwed a null pointer in this code.

My report is, sonar seems to do not analice correctly the code when a Optional object is used.

Changin this code

final FraEnvio010 fraEnvio010 = Optional.ofNullable(envio010).orElse(new FraEnvio010());

for this other code:

final FraEnvio010 fraEnvio010;

    if (Objects.isNull(envio010)) {
        fraEnvio010 = new FraEnvio010();
    } else {
        fraEnvio010 = envio010;
    }

the issue dissapears.

The versión of the Sonar Qube.

  • Community Edition
  • Version 7.4 (build 18908)

If you need more information about our sonar qube version, please ask for him and i will inform asap.

1 Like

Hi,

What is the version of SonarJava installed on your SonarQube server ?

I’m getting this as well, we’re using SonarCloud.

The server is running on Java 10 JVM.

Closing the topic for lack of traction. If you are still experiencing a similar problem. Please open a new thread with a clear, self-contained, compiling reproducer, and the version of SonarQube and Analyzer for Java you are using.