stanfordnlp/CoreNLP · error · IllegalArgumentException
Map must have at least one element to infer numeric type…
Error message
Map must have at least one element to infer numeric type; add an element first or use e.g. fromMap(map, Integer.class)
What it means
Counters.fromMap(Map<E,N>) returns a Counter view of a Map and infers the value's numeric type (Integer, Double, Long, ...) from the first element of map.values(). The library throws this IllegalArgumentException when the map is empty because there is no element from which to infer the numeric class.
Solutions
- Use the explicit-type overload: Counters.fromMap(map, Double.class) (or Integer.class etc.)
- Guard with !map.isEmpty() before calling the inferring overload
- Initialize the map with a default/seed entry if an empty map is legitimate
Example fix
// before
Counter<String> c = Counters.fromMap(map); // throws if map is empty
// after
Counter<String> c = map.isEmpty()
? new ClassicCounter<>()
: Counters.fromMap(map, Double.class); Defensive patterns
Strategy: validation
Validate before calling
Counter<E> c = map.isEmpty()
? new ClassicCounter<>()
: Counters.fromMap(map, Double.class); // explicit type, no inference needed Type guard
boolean canInfer = map != null && !map.isEmpty();
Try / catch
try {
return Counters.fromMap(map);
} catch (IllegalArgumentException e) {
log.warn("Empty map, returning empty counter");
return new ClassicCounter<>();
} Prevention
- Always prefer the explicit fromMap(map, Class) overload
- Check map size after filtering/parsing steps before conversion
- Return ClassicCounter.EMPTY or a fresh counter for empty inputs
When it happens
Trigger: Calling Counters.fromMap(someMap) where someMap.isEmpty() is true.
Common situations: Result of an empty DB/file parse passed straight to fromMap; initializing a counter before loading data; a filtered map that ended up with zero entries at runtime.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- Empty index
- Could not compute span from tokens!
- Index not large enough to name all the array elements!
- Unrecognized numeric type in wrapped counter
- Empty PQ
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/508af9769248154e.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/stats/Counters.java:2602
public static <E> Counter<E> asCounter(FixedPrioritiesPriorityQueue<E> p) {
FixedPrioritiesPriorityQueue<E> pq = p.clone();
ClassicCounter<E> counter = new ClassicCounter<>();
while (pq.hasNext()) {
double priority = pq.getPriority();
E element = pq.next();
counter.incrementCount(element, priority);
}
return counter;
}
/**
* Returns a counter view of the given map. Infers the numeric type of the
* values from the first element in map.values().
*/
@SuppressWarnings("unchecked")
public static <E, N extends Number> Counter<E> fromMap(final Map<E, N> map) {
if (map.isEmpty()) {
throw new IllegalArgumentException("Map must have at least one element" + " to infer numeric type; add an element first or use e.g." + " fromMap(map, Integer.class)");
}
return fromMap(map, (Class) map.values().iterator().next().getClass());
}
/**
* Returns a counter view of the given map. The type parameter is the type of
* the values in the map, which because of Java's generics type erasure, can't
* be discovered by reflection if the map is currently empty.
*/
public static <E, N extends Number> Counter<E> fromMap(final Map<E, N> map, final Class<N> type) {
// get our initial total
double initialTotal = 0.0;
for (Map.Entry<E, N> entry : map.entrySet()) {
initialTotal += entry.getValue().doubleValue();
}
// and pass it in to the returned inner class with a final variable
final double initialTotalFinal = initialTotal;View on GitHub (pinned to 1b7edd19c4)