{"record":{"id":"9801f346bb9fc998","repo":"Blankj/AndroidUtilCode","slug":"comparator-must-not-be-null","errorCode":null,"errorMessage":"comparator must not be null","messagePattern":"comparator must not be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/MapUtils.java","lineNumber":69,"sourceCode":"\n    @SafeVarargs\n    public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(final Pair<K, V>... pairs) {\n        LinkedHashMap<K, V> map = new LinkedHashMap<>();\n        if (pairs == null || pairs.length == 0) {\n            return map;\n        }\n        for (Pair<K, V> pair : pairs) {\n            if (pair == null) continue;\n            map.put(pair.first, pair.second);\n        }\n        return map;\n    }\n\n    @SafeVarargs\n    public static <K, V> TreeMap<K, V> newTreeMap(final Comparator<K> comparator,\n                                                  final Pair<K, V>... pairs) {\n        if (comparator == null) {\n            throw new IllegalArgumentException(\"comparator must not be null\");\n        }\n        TreeMap<K, V> map = new TreeMap<>(comparator);\n        if (pairs == null || pairs.length == 0) {\n            return map;\n        }\n        for (Pair<K, V> pair : pairs) {\n            if (pair == null) continue;\n            map.put(pair.first, pair.second);\n        }\n        return map;\n    }\n\n    @SafeVarargs\n    public static <K, V> Hashtable<K, V> newHashTable(final Pair<K, V>... pairs) {\n        Hashtable<K, V> map = new Hashtable<>();\n        if (pairs == null || pairs.length == 0) {\n            return map;\n        }","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/MapUtils.java#L51-L87","documentation":"MapUtils.newTreeMap() creates a TreeMap populated with key-value pairs, requiring a non-null Comparator to define ordering. Passing a null comparator throws IllegalArgumentException immediately because the method signature promises ordering via the comparator — there is no natural-ordering fallback in this overload.","triggerScenarios":"Calling MapUtils.newTreeMap(null, pair1, pair2) or passing a comparator variable that resolved to null (e.g., a conditionally-assigned field or a method return that produced null).","commonSituations":"Developer passes null expecting natural ordering (unaware this overload requires a comparator); a comparator obtained from configuration or a map lookup returns null; refactor that changed a non-null comparator to a nullable one.","solutions":["For natural ordering, use MapUtils.newTreeMap(Comparator.naturalOrder(), pairs) or construct new TreeMap<>() directly.","Ensure the comparator is non-null before the call — validate or provide a default: comparator = comparator != null ? comparator : Comparator.naturalOrder().","If you need a TreeMap without a custom comparator, don't use this overload — use the standard TreeMap constructor."],"exampleFix":"// before\nTreeMap<String, Integer> map = MapUtils.newTreeMap(null, Pair.create(\"a\", 1));\n\n// after\nTreeMap<String, Integer> map = MapUtils.newTreeMap(\n    Comparator.naturalOrder(), Pair.create(\"a\", 1));","handlingStrategy":"validation","validationCode":"// Ensure comparator is non-null before calling\nComparator<String> cmp = getCmp();\nif (cmp == null) cmp = Comparator.naturalOrder();\nTreeMap<String, Integer> map = MapUtils.newTreeMap(cmp, Pair.create(\"a\", 1));","typeGuard":"// N/A — this is a null-value check, not a type check\nstatic <K> Comparator<K> safeComparator(Comparator<K> cmp) {\n    return cmp != null ? cmp : Comparator.naturalOrder();\n}","tryCatchPattern":"try {\n    map = MapUtils.newTreeMap(comparator, pairs);\n} catch (IllegalArgumentException e) {\n    // comparator was null — retry with natural ordering\n    map = MapUtils.newTreeMap(Comparator.naturalOrder(), pairs);\n}","preventionTips":["Always pass Comparator.naturalOrder() instead of null when you want natural ordering.","Validate comparators before passing: use Objects.requireNonNullElse(cmp, Comparator.naturalOrder()).","For null-safe TreeMap construction, use new TreeMap<>() directly and putAll().","Document which overloads require non-null arguments."],"tags":["java","android","map","null-check","validation"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}