Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » erroneous "Potential null pointer access" warning
erroneous "Potential null pointer access" warning [message #556589] Wed, 01 September 2010 18:21 Go to next message
Eclipse UserFriend
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 #556622 is a reply to message #556589] Thu, 02 September 2010 02:59 Go to previous messageGo to next message
Eclipse UserFriend
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.

Dani
>
> Thanks.
>
Re: erroneous "Potential null pointer access" warning [message #556811 is a reply to message #556622] Thu, 02 September 2010 12:53 Go to previous messageGo to next message
Eclipse UserFriend
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?

Brian
Re: erroneous "Potential null pointer access" warning [message #556892 is a reply to message #556589] Fri, 03 September 2010 00:35 Go to previous messageGo to next message
Eclipse UserFriend
This is not a bug in my opinion.

Static analysis in the compiler doesn't compute values of expressions, but only take into account the initilization status and null status of variables.

In this case, when u write
boolean bNotNull = (b != null);

we flag b as being potentially null as the code compares b with null. But we dont calculate true/false status of bNotNull. Again in the line

return (aNull && bNotNull) ? b.length() : 42;

when the compiler analyzed b.length, it find the null status of b in the flow info as potentially null, hence flagging off the warning.
Re: erroneous "Potential null pointer access" warning [message #556899 is a reply to message #556811] Fri, 03 September 2010 02:07 Go to previous messageGo to next message
Eclipse UserFriend
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.

Dani
>
> Brian
Re: erroneous "Potential null pointer access" warning [message #556983 is a reply to message #556899] Fri, 03 September 2010 09:53 Go to previous messageGo to next message
Eclipse UserFriend
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! Smile

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 Go to previous message
Eclipse UserFriend
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.
>
Previous Topic:Autocompletion implementaion
Next Topic:Poor junit performance with threaded tests in 3.6
Goto Forum:
  


Current Time: Mon Aug 11 18:43:26 EDT 2025

Powered by FUDForum. Page generated in 3.89591 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top