Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 321973

Summary: "null comparison always yields false" although the variable can be null
Product: [Eclipse Project] Platform Reporter: Pascal Pollet <paspol>
Component: RuntimeAssignee: platform-runtime-inbox <platform-runtime-inbox>
Status: RESOLVED INVALID QA Contact:
Severity: normal    
Priority: P3    
Version: 4.0   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Pascal Pollet CLA 2010-08-06 07:43:24 EDT
Build Identifier: 20090920-1017

in this minimal example, the statement "if (zipFile == null)" produces the error message: "Null comparison always yields false: The variable zipFile cannot be null at this location". But zipFile can be null at this location!

This happens when the compiler warning "redundant null check" is enabled in the eclipse preferences.

simpleTest.java:

import java.io.File;

public class SimpleTest
{

    public static void main(String[] args) throws Exception
    {
        new SimpleTest();
    }

    public SimpleTest() throws Exception
    {
        File zipFile = null;

        double answer = Math.random();

        if (answer > 0.5)
        {
            zipFile = new Test().getFile();
        }
        else
        {
	    return;
	}

        if (zipFile == null)
        {
            return;
        }
    }
}

Test.java:

import java.io.File;


public class Test
{

    public File getFile()
    {
        File paketFile = new File("bla");

        if (paketFile.exists())
        {
            return paketFile;
        }

        return null;
    }
}


Reproducible: Always

Steps to Reproduce:
1. Paste my minimal example into eclipse :)
2.
3.
Comment 1 Pascal Pollet CLA 2010-08-06 08:09:06 EDT
Excuse me, there is no bug, the error came because in my original test case I had a supplementary statement in the code, which I removed when I pasted the code here, because I thought it was unnecessary. But this statement was the legitimate cause for the error message in eclipse:

Below the statement 

zipFile = new Test().getFile();

I had the following line:

System.out.println(zipFile.getAbsolutePath());

it is clear that this statement would throw a null pointer exception if zipFile is null, so it is correct that the code below cannot be reached in this case. 

Sometimes Eclipse is smarter than me ;)
Comment 2 DJ Houghton CLA 2010-08-06 09:29:16 EDT
Closing.