didi/DoKit · error · IllegalArgumentException
comparator must not be null
Error message
comparator must not be null
What it means
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').
Source
Thrown at Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/MapUtils.java:69
@SafeVarargs
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(final Pair<K, V>... pairs) {
LinkedHashMap<K, V> map = new LinkedHashMap<>();
if (pairs == null || pairs.length == 0) {
return map;
}
for (Pair<K, V> pair : pairs) {
if (pair == null) continue;
map.put(pair.first, pair.second);
}
return map;
}
@SafeVarargs
public static <K, V> TreeMap<K, V> newTreeMap(final Comparator<K> comparator,
final Pair<K, V>... pairs) {
if (comparator == null) {
throw new IllegalArgumentException("comparator must not be null");
}
TreeMap<K, V> map = new TreeMap<>(comparator);
if (pairs == null || pairs.length == 0) {
return map;
}
for (Pair<K, V> pair : pairs) {
if (pair == null) continue;
map.put(pair.first, pair.second);
}
return map;
}
@SafeVarargs
public static <K, V> Hashtable<K, V> newHashTable(final Pair<K, V>... pairs) {
Hashtable<K, V> map = new Hashtable<>();
if (pairs == null || pairs.length == 0) {
return map;
}View on GitHub (pinned to 626827cddb)
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().
Example fix
// before TreeMap<String, Integer> m = MapUtils.newTreeMap(null, p1, p2); // throws // after TreeMap<String, Integer> m = MapUtils.newTreeMap(Comparator.<String>naturalOrder(), p1, p2);
Defensive patterns
Strategy: validation
Validate before calling
Comparator<K> cmp = comparator != null ? comparator : Comparator.<K>naturalOrder(); TreeMap<K, V> m = MapUtils.newTreeMap(cmp, pairs);
Prevention
- A TreeMap needs an ordering: always supply naturalOrder() as the sensible default.
- If ordering is irrelevant, use HashMap/LinkedHashMap builders instead.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Transformation list must not be null.
- Index cannot be negative: " + index
- Entry does not exist: " + index
- Unsupported object type: " + object.getClass().getName()
- field == null
AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14).
Data as JSON: /api/errors/70333863fbbbcbbb.
Report an issue: GitHub.