Community
Participate
Working Groups
Given this snippet: Object u = new Object(); Object r = (u == null ? u : null); System.out.println(r.toString()); the compiler reports: ---------- 1. ERROR in BugXYZ.java (at line 4) Object r = (u == null ? u : null); ^ Null comparison always yields false: The variable u cannot be null at this location ---------- 2. ERROR in BugXYZ.java (at line 5) System.out.println(r.toString()); ^ Potential null pointer access: The variable r may be null at this location ---------- The 2nd error is misleading since there's no way how r can be non-null. The effect is caused by incomplete analysis of the conditional expression after finding that the condition is always false. When constructing the same using an if statement the dead branch would simply be skipped by null analysis. That's why the issue cannot be reproduced using if.
Created attachment 201357 [details] Test & fix under test This patch includes one more test, where the problem is even more relevant: by swapping "==" for "!=" we get a warning where analysis should indeed by silent. The patch fixes the issue by ConditionalExpression.computeNullStatus check the reachmodes of both branches, dropping the one that is not reachable. NullReferenceTests pass, more tests pending.
Created attachment 201437 [details] Same patch with copyrights updated All compiler and model tests passed with this patch.
Released in HEAD for 3.8M2.
Verified for 3.8M2.