Sonar : Possible null pointer dereference due to return value of called method

冷暖自知 提交于 2021-02-08 03:40:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!