Sonarlint wrong NullPointerException warning (S2259) when reusing ResponseEntity

Latest Sonarlint warns S2259 when reusing ResponseEntity’s body:

ResponseEntity<ResponseVO> response = restTemplate.postForEntity(url, entity, ResponseVO.class);
if (response.getBody() != null) {
    String url = response.getBody().getUrl(); //S2259 warning

Although body is final in HttpEntity so there can’t be any NullPointerException

1 Like

Hello @orimarko,

Thanks for raising this. I understand that in this specific case response.getBody() return value can’t change between the 2 invocations, so indeed it looks like a false-positive. That said it might make the rule more complex to support this case so we will discuss it before making a decision.

The easy workaround to get rid of the issue is to call response.getBody() only once and put it in a variable.

Hope this helps

1 Like