package org.eclipse.test; public class PotentialNullPointerAccess { public static String notNull() { return "a"; } public static boolean doNothing(boolean b){ return false; } /* * Different test cases for Potential Null Pointer Access. */ public static void caseA() { String a = notNull(); boolean isNotNull = a != null; if(isNotNull) { /* * Wrong: Fake potential null pointer access warning. * Potential null pointer access: The variable a may be null at this location * * Despite we're checking to be different from null, we get the warning. */ a.toString(); } } public static void caseB() { String a = notNull(); if(a != null) { /* * 0k: No potential null pointer access warning. */ a.toString(); } } public static void caseC() { String a = notNull(); /* * 0k: No potential null pointer access warning, the analyzer can't * guess what the outcome would be from the function. */ a.toString(); } public static void caseD() { String a = notNull(); // We know for sure it won't be null. boolean isNotNull = true; if(isNotNull) { /* * 0k: No potential null pointer access warning. */ a.toString(); } } public static void caseE() { String a = notNull(); if(doNothing(a != null)) { /* * Wrong: Fake potential null pointer access warning. * The use of a != null is wrapped inside a function call, the * outcome can be anything. */ a.toString(); } } public static void caseF() { String a = notNull(); doNothing(a != null); if(true) { /* * Wrong: Fake potential null pointer access warning. * Note that the call with a != null as parameter is unrelated to the condition. */ a.toString(); } } public static void caseG() { String a = notNull(); doNothing(false); if(true) { /* * 0k: No potential null pointer access warning. */ a.toString(); } } public static void caseH() { String a = notNull(); if(true) { /* * 0k: No potential null pointer access warning. * Taking into account caseF one could expect this case to give * the warning as well. In both cases the call, with the comparison * as parameter is unrelated to the if block. */ a.toString(); } doNothing(a != null); } public static void caseI() { String a = "a"; doNothing(a != null); if(true) { /* * 0k: No potential null pointer access warning. * Despite the check, as a is clearly not null, no warning * is issued. */ a.toString(); } } }