False positive NPC java:S2259

  • Operating system: Windows 11
  • SonarLint plugin version: 10.3.0.77475
  • Programming language you’re coding in: Java
  • Is connected mode used: False
    • Connected to SonarCloud or SonarQube (and which version):

And a thorough description of the problem / question:

SonarLint: ‘senderTeam’ is dereferenced.
SonarLint: A “NullPointerException” could be thrown; “senderTeam” is nullable here.

‘senderTeam’ cannot be null, as I am already checking its nullity with ‘warningKey’. If it is null, ‘warningKey’ is set, leading to the return statement before any operations on ‘senderTeam’ are executed.

Hi,

Welcome to the community and thanks for this report!

The way I read this code, if game.getMode() == GameMode.SOLO then that’s the branch in the if-tree that gets taken, and you’ll never check senderTeam’s nullability.

 
Ann

But then warningKey is set, and the code won’t reach senderTeam.getPlayers() without checking if it’s null first.

Zrzut ekranu 2024-02-21 191931

Hi,

Yeah. Okay. :sweat_smile:

Flagging this for the language experts.

 
Ann

Hi @xeRicker,

Thank you for reporting.

I tried reproducing the problem, but it seems the issue is not raising.
Can you please provide me with a minimal reproducer so I can investigate where is the problem?

All the best,

Irina