stanfordnlp/CoreNLP · error · RuntimeException
Unrecognized numeric type in wrapped counter
Error message
Unrecognized numeric type in wrapped counter
What it means
Counters.fromMap wraps a Map's entry set in a Counter view whose mutators cast values to the concrete numeric type captured when the view was created. If setCount is called with a type that the wrapper does not recognize (not one of the handled Integer/Double/Float/Long/Short classes), it throws this RuntimeException.
Solutions
- Use a supported numeric type for the map values (Integer, Double, Float, Long, Short)
- Copy the map to a plain ClassicCounter<Double> instead of using the fromMap view
- Check the exact class passed as the second argument to fromMap — it must match map.values()' runtime type
Example fix
// before Counter<String> c = Counters.fromMap(bigDecimalMap, BigDecimal.class); // throws on setCount // after Map<String, Double> doubles = new HashMap<>(); bigDecimalMap.forEach((k, v) -> doubles.put(k, v.doubleValue())); Counter<String> c = Counters.fromMap(doubles, Double.class);
Defensive patterns
Strategy: type-guard
Validate before calling
Class<?> type = map.values().iterator().next().getClass();
Set<Class<?>> supported = Set.of(Integer.class, Double.class, Float.class, Long.class, Short.class);
if (!supported.contains(type)) {
throw new IllegalArgumentException("Unsupported numeric type: " + type);
} Type guard
static boolean isSupportedNumeric(Object v) {
return v instanceof Integer || v instanceof Double || v instanceof Float
|| v instanceof Long || v instanceof Short;
} Try / catch
try {
counterView.setCount(key, value);
} catch (RuntimeException e) {
if (e.getMessage().contains("Unrecognized numeric type")) {
throw new IllegalStateException("Use a supported numeric type (Integer/Double/Float/Long/Short)", e);
}
throw e;
} Prevention
- Stick to Integer/Double/Float/Long/Short as map value types for fromMap
- Always pass the explicit class matching the map's runtime value type
- Convert exotic numerics (BigDecimal/BigInteger/Byte) to double before wrapping
When it happens
Trigger: Calling setCount on a Counter produced by Counters.fromMap(map, typeClass) where typeClass is a numeric type outside the supported set (e.g. BigDecimal, BigInteger, Byte), or a mismatched class due to unchecked generics.
Common situations: Passing BigInteger/BigDecimal maps from math or crypto code; changing the map's value type after refactoring without updating the explicit class argument; raw-type usage that defeats generic checking.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- addFeature was called with a features object that is…
- Unexpected node class
- Unknown value for span
- Attempting to remove features based on weight from a…
- String match result must be referred to by group id
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/06beca357174c3bb.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/stats/Counters.java:2706
return entry.getValue().doubleValue();
}
public Double setValue(Double value) {
final double lastValue = entry.getValue().doubleValue();
double rv;
if (type == Double.class) {
rv = ErasureUtils.<Entry<E, Double>> uncheckedCast(entry).setValue(value);
} else if (type == Integer.class) {
rv = ErasureUtils.<Entry<E, Integer>> uncheckedCast(entry).setValue(value.intValue());
} else if (type == Float.class) {
rv = ErasureUtils.<Entry<E, Float>> uncheckedCast(entry).setValue(value.floatValue());
} else if (type == Long.class) {
rv = ErasureUtils.<Entry<E, Long>> uncheckedCast(entry).setValue(value.longValue());
} else if (type == Short.class) {
rv = ErasureUtils.<Entry<E, Short>> uncheckedCast(entry).setValue(value.shortValue());
} else {
throw new RuntimeException("Unrecognized numeric type in wrapped counter");
}
// need to call getValue().doubleValue() to make sure
// we keep the same precision as the underlying map
total += entry.getValue().doubleValue() - lastValue;
return rv;
}
};
}
public void remove() {
total -= lastEntry.getValue().doubleValue();
it.remove();
}
};
}
View on GitHub (pinned to 1b7edd19c4)