{"record":{"id":"70333863fbbbcbbb","repo":"didi/DoKit","slug":"comparator-must-not-be-null","errorCode":null,"errorMessage":"comparator must not be null","messagePattern":"comparator must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/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/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/MapUtils.java#L51-L87","documentation":"MapUtils.newTreeMap(comparator, pairs...) builds a TreeMap keyed by the supplied Comparator because a TreeMap without a natural ordering needs one. Passing null would make the TreeMap constructor itself throw NullPointerException later, so the utility fails fast with IllegalArgumentException('comparator must not be null').","triggerScenarios":"Calling newTreeMap(null, pair1, pair2), typically from Kotlin where the comparator parameter has no default, or from Java passing a conditionally-created comparator that evaluated to null.","commonSituations":"Optional-sorting code that builds a comparator only when a sort field is configured; DI/injection returning null for a comparator dependency; copy-paste from newHashMap usage where null args were tolerated.","solutions":["Pass a real comparator, e.g. naturalOrder(): newTreeMap(Comparator.<K>naturalOrder(), pairs...).","If null was meant as 'no sorting', use HashMapUtils-style newHashMap/newLinkedHashMap instead.","When comparator choice is dynamic, default it: comparator != null ? comparator : Comparator.naturalOrder()."],"exampleFix":"// before\nTreeMap<String, Integer> m = MapUtils.newTreeMap(null, p1, p2); // throws\n\n// after\nTreeMap<String, Integer> m = MapUtils.newTreeMap(Comparator.<String>naturalOrder(), p1, p2);","handlingStrategy":"validation","validationCode":"Comparator<K> cmp = comparator != null ? comparator : Comparator.<K>naturalOrder();\nTreeMap<K, V> m = MapUtils.newTreeMap(cmp, pairs);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["A TreeMap needs an ordering: always supply naturalOrder() as the sensible default.","If ordering is irrelevant, use HashMap/LinkedHashMap builders instead."],"tags":["collections","treemap","null-safety"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}