You first test if getBody()
returns null
.
SonarQube sees that and thinks the method can return null.
Then, you call the same method again. SonarQube just knows that the method can return null so it tells you the warning.
In other words, SonarQube thinks that it could return something different than null
the first time but return null
the second time.
If the method is just a simple getter and the object is not modified concurrently, this is no problem.
In order to remove the warning, you can do one of the following:
- Save the result of
getBody()
to a variable, check if it is null and continue if it is not
- Add a
//NOSONAR
comment telling SonarQube that you know what you are doing.
Note that you might want to explain why this is ok in the comment, if you decide for the second case.
holdingResponseEntity.getBody() != null
andholdingResponseEntity.getBody().getError() !=null
. The first one is oke but the second one can throw NPE because the second one callgetBody()
again, and result can be null in theory. Unless you're checking like this(body=holdingResponseEntity.getBody()) != null && body.getError() != null
– Honna