Community
Participate
Working Groups
Build Identifier: 20100917-0705 I get a "Null comparison always yields false: The variable s cannot be null at this location" in the following code. That should not be the case here. The difference between the good and the bad test case is only one if. public class NullTest { public static void main(String[] args) { good(args); bad(args); } public static void good(String[] args) { String s = ""; for (int i = 0; i < 13; i++) { s = test(args, i); if (s == null) { System.out.println("null"); } } } public static void bad(String[] args) { String s = ""; for (int i = 0; i < 13; i++) { if (i != 0) { s = test(args, i); } if (s == null) { System.out.println("null"); } } } public static String test(String[] args, int i) { if (args.length > i) { return args[i]; } return null; } } Reproducible: Always Steps to Reproduce: 1. Put the code into Eclipse. 2. Have all the code warnings on. 3. See bogus warning about unneccessary null check.
I'll take a look.
I have essentially the same problem, with essentially the same reproduction test case. If the initial line = "" is changed to line= null then the warning goes away. It does not change based on whether the initial doRead is set to true or false. package net.wagland.paul.test; import java.io.BufferedReader; import java.io.IOException; public class NullWarningTest { public void testNullWarning(BufferedReader bufReader) throws IOException { String line = ""; boolean doRead = false; while (true) { if (doRead) line = bufReader.readLine(); if (line == null) return; doRead = true; } } }
Actually, this bug should be marked as a duplicate of bug 313870.
Closing as dup *** This bug has been marked as a duplicate of bug 313870 ***
Verified for 3.7 RC1 with build I20110514-0800.