简体   繁体   English

Sonarqube说:可能会抛出“ NullPointerException”。 “ getFolderPath()”可以返回null

[英]Sonarqube says: A “NullPointerException” could be thrown; “getFolderPath()” can return null

I get a following bug from sonarqube 我从sonarqube得到以下错误

A "NullPointerException" could be thrown; 可能会抛出“ NullPointerException”。 "getFolderPath()" can return null. “ getFolderPath()”可以返回null。

for the following line: 对于以下行:

if (upstreamContent.getFolderPath() != null && !upstreamContent.getFolderPath().isEmpty()) {
   ...
}

Where upstreamContent may not be null. 其中上游内容不能为空。

How can I fix it in sonarqube without turning off the rule? 我如何在不关闭规则的情况下用声纳法修复它?

UPDATE: 更新:

Someone asked me to show the source of UpstreamContent 有人要求我显示UpstreamContent的来源

public class UpstreamContent {

    @Nullable
    private String folderPath;

    ...

    @CheckForNull
    @Nullable
    public String getFolderPath() {
        return folderPath;
    }

    public void setFolderPath(String folderPath) {
        this.folderPath = folderPath;
    }
}

Probably by saving the result to a variable rather than making the call twice: 可能是通过将结果保存到变量而不是两次调用:

String path = upstreamContent.getFolderPath();
if (path != null && !path.isEmpty()) {
   ...
}

Maybe you or I know that if it doesn't return null the first time, it won't the second (I don't, actually, because I don't know what upstreamContent is :-) ), but a general-purpose tool can't know or assume that. 也许您或我知道,如果它第一次不返回null ,就不会第二次(实际上,我不知道,因为我不知道upstreamContent是什么:-)),而是通用的工具无法知道或假设。 For all it knows, it could return non- null the first time and null the second. 对于所有它知道,它可能返回非null的第一次和null第二。 By saving the call result to a variable, we ensure that Sonarqube and anyone coming along later to read and maintain the code knows, for sure, that we've done a null check before calling isEmpty . 通过将调用结果保存到变量中,我们可以确保Sonarqube和以后要阅读和维护代码的任何人都知道,在调用isEmpty之前,我们已经进行了null检查。

In fact, now that you've posted the code for upstreamContent 's class, it's entirely possible for getFolderPath to return non- null for the first call and null for the second (if another thread calls setFolderPath(null) in-between). 事实上,现在你已经发布的代码upstreamContent的类,它是完全有可能的getFolderPath返回非null的第一个呼叫, null (如果另一个线程调用第二setFolderPath(null)在中间)。

暂无
暂无

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

相关问题 SonarQube - 可能会抛出“NullPointerException”; - SonarQube - A “NullPointerException” could be thrown; 可能会抛出“NullPointerException”; “getBody”可以返回 null 声纳 - A “NullPointerException” could be thrown; “getBody” can return null Sonar 声纳问题:可能会抛出“NullPointerException”; “getDataSource()”可以返回 null。 如何在尝试使用资源块时解决 - Sonar issue: A "NullPointerException" could be thrown; "getDataSource()" can return null. How to solve in try with resources block SonarQube 可能抛出 NullPointerException; Conn 在这里可以为空 - SonarQube A NullPointerException could be thrown; Conn is nullable here SonarQube Java if语句中可能抛出“ NullPointerException” - SonarQube Java 'NullPointerException might be thrown' in if statement 字段不为null时抛出nullpointerexception - nullpointerexception thrown while fields are not null 抛出NullPointerException,但显然不是null - NullPointerException being thrown, but is clearly not null 尽管检查了null,但为什么源代码分析器声称NullPointerException可能会被抛出 - Why does a source code analyzer claim a NullPointerException could be thrown despite a check for null SonarQube 在空检查中说“被测试覆盖(4 个条件中的 3 个)” - SonarQube says “Covered by tests (3 of 4 conditions)” on a null check 抛出NullPointerException,无法抛出它 - NullPointerException thrown in where it can't be thrown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM