我收到错误消息:“在访问值之前调用“Optional#isPresent()””
但是正如您在图像中看到的那样,该行之前有一个 isPresent() 检查。
这是 SonarQube 的错误吗?
-
最佳答案
ernest_k 是对的:(谢谢!)
Strictly speaking, when you call component.getId() for the second time, you can't assume that it will give the same Optional instance you called isPresent() on.
所以我将代码更改为:
Optional<String> optionalId = component.getId();
if (optionalId.isPresent()) {
String id = optionalId.get();
// ...
}
关于java - SonarQube 8.1.0 提示在访问该值之前调用 "Optional#isPresent()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59502557/