【问题标题】: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此方法调用为非空方法参数传递空值。参数被注释为应始终为非空的参数
【发布时间】:2018-12-14 01:29:02
【问题描述】:

以下方法的 SonarQube 错误,任何关于如何解决问题的建议专家 - 此方法调用为非空方法参数传递空值。要么参数被注释为应始终为非空的参数,要么分析表明它将始终被取消引用。

    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);
    }

【问题讨论】:

    标签: java sonarqube


    【解决方案1】:

    问题在于return new ByteArrayResource(content); 语句位于try/catch 块之外。由于您的方法是抛出IOException,因此您不应该抓住它。下面应该解决它:

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

    【讨论】:

      猜你喜欢
      • 2019-05-16
      • 2022-10-07
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 2016-03-27
      • 2020-10-24
      • 2019-06-03
      相关资源
      最近更新 更多