Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 411229

Summary: [null][correlation] Invalid 'Potential null pointer access: The variable bar may be null at this location'
Product: [Eclipse Project] JDT Reporter: Etienne Reichenbach <etienner>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED DUPLICATE QA Contact:
Severity: enhancement    
Priority: P3 CC: garydgregory, sebastian.zarnekow, shankhba, stephan.herrmann
Version: 4.2.1Keywords: helpwanted
Target Milestone: 4.7 M1   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Etienne Reichenbach CLA 2013-06-20 07:16:02 EDT
I get a false "Potential null pointer access":

	private void foo(boolean bool) {
		Integer bar= null;
		if (bool) {
			bar= bar(); // just a method returning an Integer
			if (bar == null) {
				return;
			}
		}

		if (bool) {
			System.out.println(bar.byteValue());
		}


	}
Comment 1 Etienne Reichenbach CLA 2013-06-20 07:21:45 EDT
1 I get a false "Potential null pointer access":
2 
3 	private void foo(boolean bool) {
4 		Integer bar= null;
5 		if (bool) {
6 			bar= bar(); // just a method returning an Integer
7 			if (bar == null) {
8 				return;
9 			}
10		}
11 
12 		if (bool) {
13 			System.out.println(bar.byteValue());
14 		}
15 
16 
17 	}

The warning is at line 13...
Comment 2 Stephan Herrmann CLA 2013-06-20 08:03:11 EDT
I understand your surprise, but the observed behavior is actually as I'd expect:

First, in the general case where there could be more statements between the two
if-statements - the value of bool could change from false to true, which means
we could enter the second then-branch without touching the first then-branch,
thus seeing bar in its initial state being null. -> To see that bar cannot be null
the compiler would need to know that bool doesn't change its value between the
two ifs. However, we have no analysis for this (unless you'd declare bool as final).

Second, and more importantly, the compiler has no capabilities to do correlation
analysis, but that's what we'd need here: to work with the knowledge that 
    *if* bool is true *then* bar is not-null
By contrast we only see that bar is not-null on one branch and null on the other, but
in line 12 we no longer know about the conditions leading to one branch or the other.
-> I'll mark this bug with [correlation] in case some time in the future somebody
is going to work on such correlation analysis. Until then there's nothing we can do.


However, for now I suggest you add just one line before line 13 to tell the compiler
what you found out by careful (human) analysis:
   assert bar != null;
For this to have an effect please enable the option
   "Include 'assert' in null analysis"
With this the compiler can leverage your insights and continue from there without
giving the warning you know to be useless.
Comment 3 Sebastian Zarnekow CLA 2013-06-20 08:11:14 EDT
Stefan, you mean the value of the parameter 'bool' changes without an assignment in the method?
Comment 4 Stephan Herrmann CLA 2013-06-20 10:53:41 EDT
(In reply to comment #3)
> Stefan, you mean the value of the parameter 'bool' changes without an
> assignment in the method?

I certainly didn't mean that :)

(In reply to comment #2)
> First, in the general case where there could be more statements between the
> two if-statements - the value of bool could change from false to true

Anything unclear?

For non-final variables the analysis simply has no notion of "var x has not
changes since statement y", hence the two evaluations of 'bool' don't indicate
identical values - to the null analysis.
Comment 5 Etienne Reichenbach CLA 2013-06-20 11:07:23 EDT
Thanks for the answer Stephan. Looking forward that someone implements the correlation analysis :-)

Meanwhile I'm totally happy with the assert workaround.

To people wondering how to enable the "Include 'assert' in null analysis":
go to Preferences->Java->Compiler->Error/Warnings there you find the "Include 'assert' in null analysis" checkbox
Comment 6 Gary D. Gregory CLA 2016-02-18 13:38:40 EST
IMO, this should be fixed since all vars involved are final:

1    private void test(final String s) {
2        final boolean b = s != null;
3        final int x = b ? s.length() : 22;
         // ...
     }

Warning in line 3: Potential null pointer access: The variable s may be null at this location. 

But s CANNOT be assigned at all.
Comment 7 Gary D. Gregory CLA 2016-02-18 13:41:01 EST
I am on 4.5.1 BTW.
Comment 8 Stephan Herrmann CLA 2016-02-18 13:52:50 EST
(In reply to Gary D. Gregory from comment #6)
> IMO, this should be fixed since all vars involved are final:

Feel free to propose a patch.

For fairness: the reason why we have implemented no correlation analysis is: complexity. So far we have no infrastructure for any of this. To make things worse: any infrastructure for correlation analysis would have to be integrated with a pretty tricky encoding of null-related flow information.

I'd be happy to give some initial pointers, though, if someone would like to experiment towards this enhancement.
Comment 9 Stephan Herrmann CLA 2016-06-28 17:18:10 EDT
Bulk closing all compiler bugs tagged [null][correlation], because we have no plans to add such a feature: it would be a tremendous implementation effort, beyond our current man power, and may be impossible to achieve within the desired performance bounds.

If s.o. has a viable strategy or even implementation for such a feature, I'm all ears.
Comment 10 Jay Arthanareeswaran CLA 2016-08-03 07:58:29 EDT
Verified for 4.7 M1.
Comment 11 Stephan Herrmann CLA 2018-08-30 10:30:29 EDT
I created a new umbrella RFE outlining what would be needed to address this issue.

*** This bug has been marked as a duplicate of bug 538421 ***