我有以下代码:
String schemaPath = (Objects.requireNonNull(methodParameter).getParameterAnnotation(ValidJson.class).value());
SonarQube 将此标记如下: 可能会抛出“NullPointerException”; “getParameterAnnotation()”可以返回 null。
我已经尝试了以下方法,但在 sonarcube 上仍然遇到相同的错误,有什么想法吗?
try{
schemaPath = (Objects.requireNonNull(methodParameter).getParameterAnnotation(ValidJson.class).value());
} catch(NullPointerException ex){
throw new NullPointerException();
}
也尝试过这个并得到同样的错误:
String s = " ";
if (methodParameter.getParameterAnnotation(ValidJson.class).value() != null) {
s = methodParameter.getParameterAnnotation(ValidJson.class).value();
}
String schemaPath = " ";
if (s != null){
schemaPath = s;
}
我认为这可以解决问题:
String s = " ";
Object paramAnnotation = methodParameter.getParameterAnnotation(ValidJson.class);
if (paramAnnotation != null) {
s = paramAnnotation.value();
}
String schemaPath = " ";
if (s != null) {
schemaPath = s;
}