erroneous "Potential null pointer access" warning [message #556589] |
Wed, 01 September 2010 18:21  |
Eclipse User |
|
|
|
The following code
public int test() {
String a = getA();
boolean aNull = (a == null);
String b = getB();
boolean bNotNull = (b != null);
return (aNull && bNotNull) ? b.length() : 42; // <<< warning
}
public String getA() {
return "foo";
}
public String getB() {
return "bar";
}
will generate the following warning for the indicated line:
Potential null pointer access: The variable b may be null at this location.
Given the preceding code, b cannot be null at that location. Is this a bug?
Thanks.
|
|
|
|
|
|
|
Re: erroneous "Potential null pointer access" warning [message #556983 is a reply to message #556899] |
Fri, 03 September 2010 09:53   |
Eclipse User |
|
|
|
Dani Megert wrote on Fri, 03 September 2010 02:07 | Brian Vosburgh wrote:
> Dani Megert wrote on Thu, 02 September 2010 02:59
>> Brian Vosburgh wrote:
>> > The following code
>> >
>> > public int test() {
>> > String a = getA();
>> > boolean aNull = (a == null);
>> > String b = getB();
>> > boolean bNotNull = (b != null);
>> > return (aNull && bNotNull) ? b.length() : 42; // <<< warning
>> > }
>> > public String getA() {
>> > return "foo";
>> > }
>> > public String getB() {
>> > return "bar";
>> > }
>> >
>> > will generate the following warning for the indicated line:
>> >
>> > Potential null pointer access: The variable b may be null at this >
>> location.
>> >
>> > Given the preceding code, b cannot be null at that location. Is
>> this a > bug?
>> No. It's a "potential" null pointer access. In your case it's
>> obvious: the methods could return different values from subclasses.
>> But even if you would declare the methods private the compiler would
>> still warn because it does not try to fully compute the values.
>> >
>
>
> Thanks for the response, Dani.
>
> "b.length()" can only be called if "bNotNull" is true and "bNotNull"
> is calculated in line 4 of the method to be "(b != null)".
> So, I see no "potential" at all. Or am I missing something?
Yes, you are. Your class might be used in a different context where a
subclass returns 'null' in getA() and getB(). But as said before, even
if you declared your class private the compiler would not detect the
case because it does not do a full-fledged flow + value analysis.
|
Even if the subclass returns null for getB(), b.length() would never be executed, given the check that is captured in bNotNull. The value of b is never changed between "bNotNull = (b != null)" and the ternary statement controlled by the value of bNotNull. If the compiler does not perform a full-fledged "flow + value analysis" it seems it is overstepping its helpfulness with this warning?
Some sort of analysis is performed, since, if I change the ternary statement to this:
return (aNull && (b != null)) ? b.length() : 42;
the warning goes away.
I was just looking to make some code a bit a more readable, using semantically-named temporary variables. (The actual code is a bit more complicated than this example....) But then this warning popped up. Rats! 
Thanks for the help.
Brian
|
|
|
Re: erroneous "Potential null pointer access" warning [message #559482 is a reply to message #556589] |
Thu, 16 September 2010 14:36  |
Eclipse User |
|
|
|
Agreed, that's a nasty bug in Eclipse's control flow analyzis. Just
return null from getB() and you'll see that no NPE is thrown. Maybe you
want to file an enhancement request?
Regards,
Sebastian
Am 02.09.10 00:21, schrieb Brian Vosburgh:
> The following code
>
> public int test() {
> String a = getA();
> boolean aNull = (a == null);
> String b = getB();
> boolean bNotNull = (b != null);
> return (aNull && bNotNull) ? b.length() : 42; // <<< warning
> }
> public String getA() {
> return "foo";
> }
> public String getB() {
> return "bar";
> }
>
> will generate the following warning for the indicated line:
>
> Potential null pointer access: The variable b may be null at this location.
>
> Given the preceding code, b cannot be null at that location. Is this a bug?
>
> Thanks.
>
|
|
|
Powered by
FUDForum. Page generated in 0.04434 seconds