xiangtui 发表于 2013-1-26 13:35:43

2.Nullcheck of value previously dereferenced

Nullcheck of value previously dereferenced

[M C RCN] Nullcheck of value previously dereferenced
A value is checked here to see whether it is null, but this value can't be null because it was previously dereferenced and if it were null a null pointer exception would have occurred at the earlier dereference. Essentially, this code and the previous dereference disagree as to whether this value is allowed to be null. Either the check is redundant or the previous dereference is erroneous.
 
先看一段代码:
<div class="cnblogs_code">public class MyTest {
     
    private String str = "123";
    public void setStr(String str){
        this.str = str;
    }
    public String getStr(){
        return this.str;
    }

    public void test(){
        String str2 = "123";
        synchronized (str) {
            if (str != null) {
                str2 ="123";
            } else {
                str2 ="456";
            }           
            System.out.println(str2);
        }
    }
}
页: [1]
查看完整版本: 2.Nullcheck of value previously dereferenced