Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Potential null pointer access in Eclipse - seemingly trivial example

I have this very basic piece of code and Eclipse gives me the "potential null pointer access" warning/error.

public class PotentialNull {
    public void test(final String nullableString) {
        boolean isNotNull = nullableString != null;

        if (nullableString != null) {
            // No problem
            System.out.print(nullableString.hashCode());
        }

        if (isNotNull) {
            // Potential null pointer access: The variable nullable may be null at this location
            System.out.print(nullableString.hashCode());
        }
    }
}

As you can see in the example, the compiler knows that inside the first if statement, nullableString can't be null. However, in the second it doesn't and I really wouldn't have thought this would be too tough to figure out.

Can someone please confirm that this is not specific to my system/setup?

I know, I could suppress warnings etc. but I'm really wondering whether this might be a bug. Or am I overlooking something?

Another question about this seems to address a much more complex situation so I hope it's ok I'm asking again.

like image 869
Felizett Avatar asked Nov 08 '22 07:11

Felizett


1 Answers

This is described in Eclipse bug 260293 one of several duplicates closed as Won't Fix.

The bug basically says that Eclipse does not track the correlation between the variables isNotNull and nullableString so it doesn't know that the value can't be null.

like image 95
greg-449 Avatar answered Nov 15 '22 08:11

greg-449