SonarQube 扫描 - getParameterAnnotation() 可能会抛出“NullPointerException”[重复]

问题描述 投票:0回答:1

我有以下代码:

 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;
}        
java sonarqube-scan
1个回答
1
投票

我认为这可以解决问题:

String s = " ";
Object paramAnnotation = methodParameter.getParameterAnnotation(ValidJson.class);
if (paramAnnotation != null) {
    s = paramAnnotation.value();
}       

String schemaPath = " ";
if (s != null) {
    schemaPath = s;
}
© www.soinside.com 2019 - 2024. All rights reserved.