SONARJAVA-Rule S2259

Good day.

I’m trying to solve a bug with SonarLint. The rul is S2259 that references a possible null pointer exception, but I added an if conditional but the error still exists.

Version: SonarLint 5.8.1

if(pdf!=null && pdf.getOriginalFilename()!=null) {
            	try {
            		tmpStr = pdf.getOriginalFilename().substring(0, pdf.getOriginalFilename().length() - 4);
            	}catch(Exception ex) {
            		throw new GeneralException();
            	}

Hello, welcome to the community! And thanks for your message.

I believe that the rule is behaving correctly in the general case: in a multi-thread context, nothing guarantees that the value returned by pdf.getOriginalFilename() is not null on the second and third calls.

You should not get any issue raised on the code below:

if(pdf != null) {
    // Storing the result of pdf.getOriginalFilename()
    // to make sure we refer to the same value each time
    String originalFileName = pdf.getOriginalFilename();
    if (originalFileName != null) {
        try {
            tmpStr = originalFileName.substring(0, originalFileName.length() - 4);
        } catch(Exception ex) {
            throw new GeneralException();
        }
    }
}