Sonarcloud and Sonarlint don't see NullPointerException java:S2259 if Log4j2 is used

I’m trying to use sonarcloud and sonarlint to catch more NPE exceptions. To test this i created a simple service and added some clearly problematic code but both sonarcloud and sonarlint don’t see the problem with rule java:S2259 and i can’t understand why!
After some more testing i discovered that if the annotation Log4j2(lombok) is used to autogenerate the “log” object and a method uses it then that method will never trigger S2259 if i remove from the method the “log” line or i manually create the “log” object then the rules are triggered. I don’t know what is going on and why is behaving like that.
The problem seems affect only the rule S2259, other rules works. Thanks for your help :pray:

@RestController
@RequestMapping(path = "rest/template-java", produces = MediaType.APPLICATION_JSON_VALUE)
@Log4j2
public class TemplateJavaController {

 ....

  private int ruleNotTriggered() {
    String a=null;
    log.error("template");
    return a.length();
  }
  private int ruleTriggered() {
    String a=null;
    return a.length();
  }
1 Like

Hi Nicola,

Welcome to the community. That’s a great catch!

I created a ticket so we can look into this at some point to improve the analysis:
https://sonarsource.atlassian.net/browse/SONARJAVA-5030

Thank you for bringing this up and taking the time to create a small reproducer, that will help us a lot.

Feel free to share more cases if you find them!

1 Like

Thank you!

1 Like