简体   繁体   中英

Klockwork warning: Null pointer dereference of where null is returned from a method

While doing bug analysis using klocwork, I am getting warning Null pointer dereference of 'nextLineDn' where null is returned from a method .

Apparently other static analysis tool findbug also gives same warning.

But clearly i am checking for null/empty before using it.

      int noOfLines = device.getLines().size();     
        if( lineNo != 0 && noOfLines > lineNo ) // if next line exists      
        {   
            nextLineDn = device.getDn(lineNo+1);    
            if(!Util.isNullOrEmpty(nextLineDn)) 
            {       
                return (nextLineDn.contains("@")) ? nextLineDn.split("@")[0] : nextLineDn;  
            }       
        }

class Util :

public static boolean isNullOrEmpty(String str) {
    return (str == null || str.isEmpty());
}

can someone give me some idea on this? i am getting so many warning with same conditions.Dont know what else can be done to remove the warning.

Since Klocwork Insight is a static source code analysis tool, it might not be able to further decipher that you have a method named isNullOrEmpty() in Util class, wherein you are actually doing a null check. So, it is showing warnings in your IDE.

Static analysis tool tries to find potential flaws in advance. So, here Klocwork would tell : device.getDn() might return null, be careful with using nextLineDn .

But, if you put code like (nextLineDn!=null) , I guess it would not flag a warning there. (Try and let us know)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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