简体   繁体   English

findbug中可能的空指针取消引用是什么意思?

[英]What is the meaning of Possible null pointer dereference in findbug?

I am using Sonar and I have got this kind of violation from it for a peace of my code:我正在使用声纳,为了我的代码安全,我从中得到了这种违规行为:

 Correctness - Possible null pointer dereference  

Has anyone know about this rule in findbugs?有人知道 findbugs 中的这条规则吗? I searched a lot but I can not find a good sample code (in Java) which describe this rule, unfortunately findbugs site did not have any sample code or good description about this rule.我搜索了很多,但我找不到一个很好的示例代码(在 Java 中)来描述这个规则,不幸的是 findbugs 站点没有任何关于这个规则的示例代码或好的描述。

Why does this violation appear?为什么会出现这种违规行为?

It says here 这里

NP: Possible null pointer dereference (NP_NULL_ON_SOME_PATH)

There is a branch of statement that, if executed, guarantees that a null value will be dereferenced, which would generate a NullPointerException when the code is executed.有一个语句分支,如果执行,则保证将取消引用空值,这将在执行代码时生成 NullPointerException。 Of course, the problem might be that the branch or statement is infeasible and that the null pointer exception can't ever be executed;当然,问题可能是分支或语句不可行,空指针异常永远无法执行; deciding that is beyond the ability of FindBugs.决定这超出了 FindBugs 的能力。

If you would have posted some code it would be easier to answer.如果您发布了一些代码,那么回答起来会更容易。

EDIT I don't see a lot of documentation but here is one example !编辑我没有看到很多文档,但这是一个例子 Hope this helps!希望这可以帮助!

a sample code is something like this.示例代码是这样的。

String s = null ;
if (today is monday){
    s = "Monday" ;
else if (today is tuesday){
    s = "Tuesday" ;
}
System.out.println(s.length()); //Will throw a null pointer if today is not monday or tuesday.

Okay好的

This is two simple Examples : First one gives a : Possible null pointer dereference这是两个简单的例子:第一个给出:可能的空指针取消引用

1. Error
     ArrayList a = null;
     a.add(j, PointSet.get(j));
     // now i'm trying to add to the ArrayList 
     // because i'm giving it null it gives me the "Possible null pointer dereference"

2. No Error
     ArrayList a = new ArrayList<>();
     a.add(j, PointSet.get(j));
     // adding elements to the ArrayList
     // no problem

Simple ?简单的 ?

In simple language, if a variable value is assigned as null, and you try to access it with any inbuilt method like add/get.用简单的语言来说,如果一个变量值被赋值为 null,并且你尝试使用任何内置方法(如 add/get)访问它。 Then null pointer dereference issue comes with SONAR.然后空指针取消引用问题伴随着 SONAR。 Because there are changes for it go null, and throw null pointer exception.因为有变化让它变成空,并抛出空指针异常。 Try to avoid it if possible.如果可能,尽量避免它。

For example:例如:

File file=null;
file.getName();

will throw "Possible null pointer dereference"将抛出“可能的空指针取消引用”

It may not happen directly as mentioned in the example, it can be unintentionally.它可能不会像示例中提到的那样直接发生,也可能是无意中发生的。

I got this issue with the following piece of code:-我在以下代码中遇到了这个问题:-

BufferedReader br = null;
    String queryTemplate = null;
    try {
        br = new BufferedReader(new FileReader(queryFile));
        queryTemplate = br.readLine();
    } catch (FileNotFoundException e) {
      //  throw exception
    } catch (IOException e) {
       // throw exception
    } finally {
        br.close();
    }

Here, the br BufferedReader can be null in br.close() .在这里, br BufferedReader 在br.close()可以为null However it can only be null if new BufferedReader() fails, in which case we are throwing the relevant exceptions.但是,如果new BufferedReader()失败,它只能为null ,在这种情况下,我们将抛出相关异常。

This is thus a false warning.因此,这是一个错误的警告。 Findbugs docs mention the same:- Findbugs文档提到了相同的内容:-

   This may lead to a NullPointerException when the code is executed.  
   Note that because FindBugs currently does not prune infeasible 
   exception paths, this may be a false warning.

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM