Community
Participate
Working Groups
When null type annotation is used to mark a Map's value type as @NonNull, Eclipse assumes that the return type of Map.remove(Object) includes the @NonNull annotation, and therefore cannot be null. This makes it impossible to check the return value for null (indicating the map did not contain the given key) without generating bogus warnings. Example: @NonNullByDefault({}) public class NullTest { public static void main(String[] args) { Map<Integer, @NonNull String> map = new HashMap<>(); String val = map.remove("badkey"); if (val == null) { // WARNING 1 System.out.println("NULL"); // WARNING 2 } } } The above code will generate two bogus warnings: WARNING 1: Null comparison always yields false: The variable val cannot be null at this location WARNING 2: Dead code However, running the code will print "NULL".
Update: This also applies to Map.get(Object), Map.put(K, V), and any other method with return type V that are intended to return null in certain cases.
Thanks for the report. Please see bug 437881 comment 1. *** This bug has been marked as a duplicate of bug 331651 ***