java:S2259 A "NullPointerException" could be thrown; False Positive

Language: Java 17
Rule: java:S2259 A “NullPointerException” could be thrown;
False positive (it is checked for not null in the same line of code)
Product: SonarQube 10.1

public void method1(Boolean isDemo) {
  if (isDemo != null && isDemo) {
       //do something here
  }
}

The rule assumes correctly that isDemo is nullable(from first condition inside if), but then it complaints that it is dereferenced in the second condition.

Hello Sebastian,

Thank you for your report.

Unfortunately, I have not been able to reproduce this issue with the information you have provided so far. This rule uses symbolic execution to perform a data flow analysis and can be affected by seemingly unrelated code close by.

Could you provide a reproducer that includes the entire file’s code and where you can trigger the issue on your side? You can provide this here, or, even better, you can provide the link to a project on e.g. GitHub.

In the meantime, if the rule causes too much noise, you can opt to disable it until the problem has been fixed.

Map<String, CenteraCopyStatus> statusesMap = centeraService.findByClipId(clipId)
                .stream()
                .collect(Collectors.toMap(CenteraCopyStatus::getFilename, Function.identity()));
        if (Optional.ofNullable(statusesMap).map(Map::values).filter(Predicate.not(Collection::isEmpty))
                .map(l -> l.stream().allMatch(CenteraCopyStatus::hasBeenProcessed))
                .orElse(false)) {
            log.info("ClipId " + clipId + " was already processed");
            return;
        }

        int count = 0;
        CenteraFolder centeraFolder = builder.readFolder(clipId, getCenteraDomain());

        try (CenteraFileIterator it = (CenteraFileIterator) centeraFolder.iterator()) {
            monitoringDataFactory.getStartedBuilder().build(data, Optional.empty());

            while (it.hasNext()) {
                CenteraFile file = it.next();
                log.info("Number of file: {}", ++count);

                String filePath = fileShareService.getFilepath(clipId, file);

               // wrong report here !
                CenteraCopyStatus status = statusesMap.get(filePath);
....

statusesMap never can be null in this case ! Sonar report wrong diagnose