java 声纳严重违规 - 先前取消引用的值的空检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26722528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:28:23  来源:igfitidea点击:

Sonar critical violation - Nullcheck of value previously dereferenced

java

提问by rickygrimes

For the below piece of code I have in one of my test classes, Sonar throws me a critical violation - Correctness - Nullcheck of value previously dereferenced

对于我在其中一个测试类中的以下代码段,Sonar 向我抛出了严重违规 - 正确性 - 先前取消引用的值的空检查

 if (testLst != null && !testLst.isEmpty()) {
        for (Test test : testLst) {
            if (test.getName().equalsIgnoreCase("TEST")) {
            // do blah
            }

Can someone throw some light on this on what am I doing wrong here?

有人可以解释一下我在这里做错了什么吗?

EDIT: One of the answers here suggested this is because I could have accessed the variable before, and so the null check is redundant. That's not true though. Here is the line of code before my null check.

编辑:这里的答案之一表明这是因为我之前可以访问该变量,因此空检查是多余的。然而事实并非如此。这是我的空检查之前的代码行。

 testLst = myTest.getValues(); //I am basically populating the array by doing a get, but I am not accessing the list itself by doing a get on it directly - like testLst.get()
 if (testLst != null && !testLst.isEmpty()) {
            for (Test test : testLst) {
                if (test.getName().equalsIgnoreCase("TEST")) {
                // do blah
                }

回答by M Anouti

This message is shown when you're checking if a variable's value is null (in this case testLst) whereas you already accessed the variable before. The null check is not needed since if the value was null, a NullPointerExceptionwould have been thrown.

当您检查变量的值是否为空(在本例中testLst)而您之前已经访问过该变量时,会显示此消息。不需要空检查,因为如果值为空,NullPointerException则会抛出 a。

Example:

例子:

testLst.remove(something);
if (testLst != null && !testLst.isEmpty()) {
    for (Test test : testLst) {
       if (test.getName().equalsIgnoreCase("TEST")) {
        // do blah
        }

The check testLst != nullis redundant since at the time the program reaches the ifstatement, testLstcannot be null, otherwise the previous statement testLst.remove(something)would have thrown a NullPointerException. In this case, you should place the null check before accessing testLst, in a place where it canbe null:

检查testLst != null是多余的,因为在程序到达if语句时,testLst不能为空,否则前一个语句testLst.remove(something)会抛出一个NullPointerException. 在这种情况下,您应该在访问之前将空检查testLst放在可以为空的地方:

if(testLst != null) {
   testLst.remove(something);
   if (!testLst.isEmpty()) {
       for (Test test : testLst) {
          if (test.getName().equalsIgnoreCase("TEST")) {
           // do blah
          }

回答by Chry007

I know I am too late for OP but maybe someone else might find this helpful:

我知道我对 OP 来说太晚了,但也许其他人可能会觉得这有帮助:

Sonarqube throws this error at the line, that would cause the initial NPE, not the line that includes the redundant null check (the error message indicates otherwise)

Sonarqube 在该行抛出此错误,这将导致初始 NPE,而不是包含冗余空检查的行(错误消息另有指示)

For OP:

对于 OP:

testLst = myTest.getValues();

I am guessing getValues() neverer returns null therefore the testLst cannot be null but only empty at the point of the null check.

我猜 getValues() 永远不会返回 null 因此 testLst 不能为 null 而只能在 null 检查时为空。

回答by Micka?l Guerin

testLst = myTest.getValues(); //I am basically populating the array by doing a get, but I am not accessing the list itself by doing a get on it directly - like testLst.get()
if (testLst != null && !testLst.isEmpty()) {
    for (Test test : testLst) {
        if (test.getName().equalsIgnoreCase("TEST")) {
        // do blah
        }

Did you checked myTest != null ? Otherwise sonar will throw the critical violation I guess.

你检查了 myTest != null 吗?否则声纳会抛出严重违规我猜。