Community
Participate
Working Groups
Build ID: M20071023-1652 Try the following code. import java.util.Random; import junit.framework.TestCase; public class Test extends TestCase { private boolean[] getArray() { if (new Random().nextFloat() < 0.5) { return null; } else { return new boolean[] { true }; } } public void testNull() { final boolean[] array = getArray(); if (array == null) { fail("null"); } for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } } } That code triggers a "Potential null pointer access" on the array.length statement. However, if array == null then the method will have already terminated by calling TestCase.fail(String). This is similar to Bug 132875 but not exactly the same.
Thanks for your suggestion. I am afraid our current plans won't allow us to pursue it though. As you noticed, this is much like bug 132875 - a P5 as of today, and would call for an even more specific pattern, tied to JUnit, or for a cross methods analysis to discover that fail will throw an unchecked exception in all cases. Note that a few workarounds can help you get rid of the unwanted warning, such as: public void testNull() { final boolean[] array = getArray(); if (array == null) { fail("null"); return; } for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } } which makes explicit to us that we exit from within the if block. Closing as WONTFIX.
Verified for 3.4M4.