Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 321973 - "null comparison always yields false" although the variable can be null
Summary: "null comparison always yields false" although the variable can be null
Status: RESOLVED INVALID
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Runtime (show other bugs)
Version: 4.0   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: platform-runtime-inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-06 07:43 EDT by Pascal Pollet CLA
Modified: 2010-08-06 09:29 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.