java - 由于被调用方法的返回值,可能在 [...] 中取消引用空指针
问题描述
关于 SonarQube 标记问题的小问题,我不明白。
我的片段非常简单。
VaultTokenResponse result = getWebClient().mutate().baseUrl(vaultUrl).build().post().retrieve().bodyToMono(VaultTokenResponse.class).block();
String vaultToken = result.getToken().getToken();
然而,在这里的第二行,Sonarqube 告诉我:
findbugs:NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE Style - Possible null pointer dereference due to return value of called method
The return value from a method is dereferenced without a null check, and the return value of that method is one that should generally be checked for null. This may lead to a NullPointerException when the code is executed
我有点不确定这意味着什么。
最重要的是,我不知道如何解决这个问题。
请帮帮忙?
谢谢
解决方案
result.getToken()
可能返回 null。因此,当您调用时,您调用result.getToken().getToken()
的getToken()
是空引用。因此将抛出 NullPointerException。
所以你可以做类似的事情
YourClass token = result.getToken();
if(token != null) {
String vaultToken = token.getToken(); // whatever you want to do with it
}
else {
// error handling
}
推荐阅读
- python - 如何根据另一个数据框行值通过连接填充一个数据框行单元格?
- android - 房间:为参数传递值
- wordpress - 带有 3 个链接数据点的 Wordpress 自定义字段
- laravel - laravel 什么时候执行 yearly() 函数
- django - Django REST 框架 - 反向 ForeignKey 关系
- java - 接口的静态和最终规则
- css - 为什么chrome浏览器在第一时间无法显示div的裁剪部分?
- java - 为什么Splitter直接返回集合在Spring Integration中处理集合的项的情况下?
- python-3.x - 有没有办法将整数列表转换为单个变量?
- python - String field comparison and print the differences