简体   繁体   English

可能会抛出“NullPointerException”; “getBody”可以返回 null 声纳

[英]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 A "NullPointerException" could be thrown; "getBody()" can return null around recaptchaResponseEntity.getBody() "getBody()" 可以在recaptchaResponseEntity.getBody()周围A "NullPointerException" could be thrown; "getBody()" can return null

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).我假设您已经运行了这些检查,但是您仍然再次调用getBody() ,这将触发问题(不能保证该方法将返回两次相同的结果)。 I would try to store getBody() return value in a variable and only use that after the check, eg:我会尝试将getBody()返回值存储在变量中,并且仅在检查后使用它,例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 声纳问题:可能会抛出“NullPointerException”; “getDataSource()”可以返回 null。 如何在尝试使用资源块时解决 - Sonar issue: A "NullPointerException" could be thrown; "getDataSource()" can return null. How to solve in try with resources block Sonarqube说:可能会抛出“ NullPointerException”。 “ getFolderPath()”可以返回null - Sonarqube says: A “NullPointerException” could be thrown; “getFolderPath()” can return null Sonar Cube“可能抛出空指针异常”:误报? - Sonar Cube “null pointer exception could be thrown” : False positive? SonarQube - 可能会抛出“NullPointerException”; - SonarQube - A “NullPointerException” could be thrown; 字段不为null时抛出nullpointerexception - nullpointerexception thrown while fields are not null 抛出NullPointerException,但显然不是null - NullPointerException being thrown, but is clearly not null 空指针取消引用问题从未在声纳中引发 - Null Pointer dereference issue never thrown in sonar 尽管检查了null,但为什么源代码分析器声称NullPointerException可能会被抛出 - Why does a source code analyzer claim a NullPointerException could be thrown despite a check for null NullPointerException可能会因为“值”为空而在此处发出声纳警告 - NullPointerException might be thrown as 'value' is nullable here sonar warning 抛出NullPointerException,无法抛出它 - NullPointerException thrown in where it can't be thrown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM