Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 22:21 Go to next message
Brian Vosburgh is currently offline Brian VosburghFriend
Messages: 137
Registered: July 2009
Senior Member
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 06:59 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
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 16:53 Go to previous messageGo to next message
Brian Vosburgh is currently offline Brian VosburghFriend
Messages: 137
Registered: July 2009
Senior Member
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 04:35 Go to previous messageGo to next message
Ayushman Jain is currently offline Ayushman JainFriend
Messages: 3
Registered: September 2010
Junior Member
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 06:07 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
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 13:53 Go to previous messageGo to next message
Brian Vosburgh is currently offline Brian VosburghFriend
Messages: 137
Registered: July 2009
Senior Member
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 18:36 Go to previous message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
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: Fri Apr 26 01:31:10 GMT 2024

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

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

Back to the top