2

I've wrote this code that is working properly, but Sonarlint warns me against a wsr.getBody().getWorkspace() that could lead to a NullPointerException on wsr.getBody(), who could yield null, it says.

public WorkspaceSummary getWorkspace(String workspaceName) {
   try {
      ResponseEntity<GetWorkspaceResponse> wsr = this.workspacesApi.getWorkspaceWithHttpInfo(workspaceName, this.quiet);

      if (wsr == null || wsr.getBody() == null) {
         throw new RuntimeException("getWorkspaceWithHttpInfo a renvoyé une réponse nulle");
      }

      return wsr.getBody().getWorkspace();
   }
   catch(HttpClientErrorException e) {
      [...]
   }
}

enter image description here

But I believe not. In my mind, I did what was necessary to prevent it.

Sometimes obvious things that are in front of us we don't see them, this is why I am asking you if I missed something.

Marc Le Bihan
  • 2,308
  • 2
  • 23
  • 41
  • 2
    dont know if this will even change anything, but do you want to try assigning wsr.getBody() to a variable, then using it in the checks and returning the variable.getWorkspace()? – experiment unit 1998X Sep 01 '23 at 06:16
  • One thing is that SonarQube is stupid. It will happily report false positives that even an average programmer sees are untrue. Suppress the warning unless you want to "code dance" to make SQ happy. – Kayaman Sep 01 '23 at 06:38
  • @Kayaman I've installed _Sonarlint_ in order to find troubles in my code. I won't desactivate its warnings. – Marc Le Bihan Sep 01 '23 at 07:40
  • I'd use the advice from @experimentunit1998X to assign it to a variable. Sonar probably can't infer that `wsr.getBody()` will always return the same result. – Tim Moore Sep 01 '23 at 08:08
  • @Kayaman you can suppress a warning of a kind on a single line, but allow it to appear elsewhere on other sources you have? I know only how to disable a warning of sme kind entierely. In my mind, if I suppress the warning "_NullPointerException could be thrown_", _Sonarlint_ won't attempt to do that detection elsewhere in my sources. – Marc Le Bihan Sep 01 '23 at 08:42
  • [Of course](https://stackoverflow.com/q/47369526/2541560). It would be pretty useless if you couldn't suppress single warnings. – Kayaman Sep 01 '23 at 08:48
  • Thanks! It can be a solution, if everything else fails. – Marc Le Bihan Sep 01 '23 at 12:24
  • @experimentunit1998X I experienced it: it works and I've wrote it for a workaround answer. – Marc Le Bihan Sep 02 '23 at 03:20

2 Answers2

1

You assume that war.getBody() is consistent and always returns the same value. Sonar looks at the code and sees that you are recalculating or refetching the value, so basically you didn’t validate the new value, so the value can be null. If you save the value than there is no threat that the value will change.

Ofer Skulsky
  • 685
  • 4
  • 10
0

The answer @oferskulsky gave can be applied the way @experimentunit1998X suggested, and it solves the warning. Great!

ResponseEntity<GetWorkspaceResponse> wsr = this.workspacesApi.getWorkspaceWithHttpInfo(workspaceName, this.quiet);
GetWorkspaceResponse response = (wsr != null) ? wsr.getBody() : null;

if (response == null) {
   throw new RuntimeException("getWorkspaceWithHttpInfo a renvoyé une réponse nulle");
}

return response.getWorkspace();
Marc Le Bihan
  • 2,308
  • 2
  • 23
  • 41