Blankj/AndroidUtilCode · error · IllegalArgumentException
comparator must not be null
Error message
comparator must not be null
What it means
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.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/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 7b4caf9e54)
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.
Example fix
// before
TreeMap<String, Integer> map = MapUtils.newTreeMap(null, Pair.create("a", 1));
// after
TreeMap<String, Integer> map = MapUtils.newTreeMap(
Comparator.naturalOrder(), Pair.create("a", 1)); Defensive patterns
Strategy: validation
Validate before calling
// Ensure comparator is non-null before calling
Comparator<String> cmp = getCmp();
if (cmp == null) cmp = Comparator.naturalOrder();
TreeMap<String, Integer> map = MapUtils.newTreeMap(cmp, Pair.create("a", 1)); Type guard
// N/A — this is a null-value check, not a type check
static <K> Comparator<K> safeComparator(Comparator<K> cmp) {
return cmp != null ? cmp : Comparator.naturalOrder();
} Try / catch
try {
map = MapUtils.newTreeMap(comparator, pairs);
} catch (IllegalArgumentException e) {
// comparator was null — retry with natural ordering
map = MapUtils.newTreeMap(Comparator.naturalOrder(), pairs);
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- u can't instantiate me...
- {}
- segment of <{segment}> is illegal
- u can't instantiate me...
- u can't instantiate me...
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/9801f346bb9fc998.
Report an issue: GitHub.