代码改变世界

[hyddd的FindBugs分析记录][M C NP] Method call passes null for unconditionally dereferenced parameter

2009-02-16 09:44  hyddd  阅读(1881)  评论(0编辑  收藏  举报

[M C NP] Method call passes null for unconditionally dereferenced parameter [NP_NULL_PARAM_DEREF]

This method call passes a null value to a method which might dereference it unconditionally.

 

这里FindBugs的解释是:你传入参数的值,有可能是一个NULL。下面看一段代码:

public boolean accept(File dir, String name) {
            String reg 
= null;
            
if (this.type == ChangeConfigUtil.windowsType) {
                reg 
= "^(\\S+)" + ChangeConfigUtil.windowsTypeTag
                        
+ "\\.(\\S*)$";
            } 
else if (this.type == ChangeConfigUtil.linuxType) {
                reg 
= "^(\\S+)" + ChangeConfigUtil.linuxTypeTag + "\\.(\\S*)$";
            }
           
            Pattern p 
= Pattern.compile(reg, Pattern.MULTILINE);  //这里reg可能为空
            Matcher m 
= p.matcher(name);
            
if (m.find()) {
                
return true;
            } 
else {
                
return false;
            }
}
这里Pattern p = Pattern.compile(reg, Pattern.MULTILINE);可能会出现问题。如果this.type既不等于windows,也不等于linux,那么reg=null,这会导致Pattern.compile(reg, Pattern.MULTILINE);抛异常。这里,FindBugs想说明的就是这个问题。