问题
if (response != null && response.getBody() != null && response.getStatusCode() == HttpStatus.OK) {
return new BigDecimal(response.getBody());
}
I am getting possible null pointer dereference due to return value of called method on above code.
Can someone please let me know the exact issue and why it's an issue?
response.getBody() // returns a string value
Thanks in advance! Please let me know if any other details are needed.
回答1:
Sonar does not know that the two consecutive calls to getBody()
will return the same value.
So, it is really possible, from the point of view of a static analyzer, that the second call returns null
.
I'd recommend assigning the body to a local variable, and calling the getter only once. Here is a reference from Sonar community, where someone reported this behavior as bug and received a similar response.
A static analyzer actually cannot prove that the two calls will return the same value, unless response
is of a final and immutable type. And no static analyzer I've tried yet goes to the length of trying to prove that.
来源:https://stackoverflow.com/questions/64170732/sonar-possible-null-pointer-dereference-due-to-return-value-of-called-method