简体   繁体   中英

A “NullPointerException” could be thrown; “getBody” can return null Sonar

Sonar points that A "NullPointerException" could be thrown; "getBody()" can return null A "NullPointerException" could be thrown; "getBody()" can return null around recaptchaResponseEntity.getBody()

public BooleanResponse verifyCaptcha(String recaptchaResponse) {

//Alot more sophisticated logic

    Boolean success = (Boolean) recaptchaResponseEntity.getBody().get("success");

    List<String> errors = (List) recaptchaResponseEntity.getBody().get("error-codes");
    return success ? BooleanResponse.success() : BooleanResponse.failure(errors.get(0));
  }

I tried

recaptchaResponseEntity.getBody()!=null , Objects.noNull(recaptchaResponseEntity.getBody()),
!Objects.isNull(recaptchaResponseEntity.getBody())

But got no luck, can you please help to resolve this issue? Thanks

Each of your example attempt do not store the compared value anywhere. I assume that you have run those checks, but then you still called getBody() again, which would trigger the issue (there is no guarantee that the method will return the same result twice). I would try to store getBody() return value in a variable and only use that after the check, eg:

<appropriate type here> recaptchaResponseBody = recaptchaResponseEntity.getBody();
if (recaptchaResponseBody != null) {
    Boolean success = (Boolean) recaptchaResponseBody.get("success");
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM