简体   繁体   English

此方法调用为非空方法参数传递空值。 将该参数注释为应始终为非null的参数

[英]This method call passes a null value for a nonnull method parameter. Either the parameter is annotated as a parameter that should always be nonnull

SonarQube error for below method, any suggestion experts on how to resolve the issue -This method call passes a null value for a nonnull method parameter. 以下方法的SonarQube错误,有关如何解决问题的任何建议专家-此方法调用将为非空方法参数传递空值。 Either the parameter is annotated as a parameter that should always be nonnull, or analysis has shown that it will always be dereferenced. 该参数被注释为应始终为非空的参数,或者分析表明它将始终被取消引用。

    public ByteArrayResource readFile() throws IOException {
        byte[] content = null;

        try (S3Object object = amazonS3.getObject(new GetObjectRequest(bucketName, key))) {
            content = IOUtils.toByteArray(object.getObjectContent());
            return new ByteArrayResource(content);

        } catch (IOException e) {
            LOG.error("IOException caught while reading file", e);
        } 
        return new ByteArrayResource(content);
    }

Problem is with return new ByteArrayResource(content); 问题是return new ByteArrayResource(content); statement outside of try/catch block. try/catch块之外的语句。 As your method is throwing IOException , you shouldn't be catching it. 当您的方法抛出IOException ,您不应该捕获它。 Below should resolve it: 下面应该解决它:

public ByteArrayResource readFile() throws IOException {
    try (S3Object object = amazonS3.getObject(new GetObjectRequest(bucketName, key))) {
        byte[] content = IOUtils.toByteArray(object.getObjectContent());
        return new ByteArrayResource(content);
    }
}

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

相关问题 为@Nonnull 注释参数编写单元测试 - Writing unit test for @Nonnull annotated parameter Android Studio错误的含义:未注释的参数会覆盖@NonNull参数 - Meaning of Android Studio error: Not annotated parameter overrides @NonNull parameter Eclipse null 分析在通用参数上有@NonNull - Eclipse null analysis has @NonNull on generic parameter 如果使用@Nonnull 进行注释,则没有针对 null 返回值的 FindBugs 警告 - No FindBugs warning for null return value if annotated with @Nonnull 如何解决 findbug 问题:为非空参数传递空值 - How to resolve findbug issue: Null passed for nonnull parameter 我可以获取方法调用的带注释参数的值吗? - Can I get the value of an annotated parameter of a method call? AspectJ - 获取带注释的方法参数的值 - AspectJ - Get value of annotated method parameter 使用AspectJ设置带注释的方法参数的值 - Using AspectJ to set the value of an annotated method parameter 声纳问题 - 参数必须是非空的,但标记为可为空 - Sonar issue - parameter must be nonnull but is marked as nullable 返回null的方法中的java @Nonnull批注 - java @Nonnull annotation in a method that returns null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM