stanfordnlp/CoreNLP · error · RuntimeException
addFeature was called with a features object that is…
Error message
addFeature was called with a features object that is neither a counter nor a collection!
What it means
The private static helper addFeature expects the features accumulator to be either a Counter<F> (setCount with weight) or a Collection<F> (add). Any other object type is a programming invariant violation, thrown as RuntimeException. This is an internal-type contract inside ColumnDataClassifier's feature extraction.
Solutions
- Ensure the features accumulator is created as a ClassicCounter<F> (for weighted features) or ArrayList<F>/Collection (for binary features)
- If you modified the code, restore the standard Counter/Collection container types in the feature extraction methods
- Cast or wrap custom containers into a Counter/Collection before calling addFeature
Example fix
// before Map<String,Double> features = new HashMap<>(); addFeature(features, feat, value); // after Counter<String> features = new ClassicCounter<>(); addFeature(features, feat, value);
Defensive patterns
Strategy: type-guard
Validate before calling
if (!(features instanceof Counter) && !(features instanceof Collection)) throw new IllegalArgumentException("features must be Counter or Collection"); Type guard
static boolean isFeatureContainer(Object o) { return o instanceof Counter<?> || o instanceof Collection<?>; } Try / catch
try { extractFeatures(...); } catch (RuntimeException e) { if (e.getMessage().contains("neither a counter nor a collection")) { log.error("Feature accumulator misconfigured", e); } else throw e; } Prevention
- Always accumulate features in ClassicCounter or a List/Collection
- After refactoring feature extraction, keep the accumulator types aligned with addFeature
- Add an assertion on the accumulator type before extraction loops
When it happens
Trigger: Internal feature-extraction code paths in ColumnDataClassifier construction that pass a features object created as neither Counter nor Collection (e.g. after refactoring the accumulator type); not normally triggerable from public API misuse.
Common situations: Subclassing/modifying ColumnDataClassifier and passing a Map or List-of-tuples as features; version drift where custom column classifiers return a non-standard container from their feature factory.
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
- Line format error at line
- Error: Line has too few tab-separated columns
- Dataset could not be loaded
- Not enough columns for format
- Unrecognized format specification in
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/d160bd85ca5fbed2.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/classify/ColumnDataClassifier.java:951
double sqrt = Math.sqrt(value);
addFeature(featuresC, "Sqrt", sqrt);
} else {
addFeature(featuresC, Flags.realValuedFeaturePrefix, value);
}
}
/**
* This method takes care of adding features to the collection-ish object features via
* instanceof checks. Features must be a type of collection or a counter, and value is used
* iff it is a counter
*/
private static <F> void addFeature(Object features, F newFeature, double value) {
if (features instanceof Counter<?>) {
ErasureUtils.<Counter<F>>uncheckedCast(features).setCount(newFeature, value);
} else if(features instanceof Collection<?>) {
ErasureUtils.<Collection<F>>uncheckedCast(features).add(newFeature);
} else {
throw new RuntimeException("addFeature was called with a features object that is neither a counter nor a collection!");
}
}
/**
* Extracts all the features from a certain input column.
*
* @param cWord The String to extract data from
* @param flags Flags specifying which features to extract
* @param featuresC Some kind of Collection or Counter to put features into
* @param goldAns The goldAnswer for this whole datum or emptyString if none.
* This is used only for filling in the binned lengths histogram counters
*/
private void makeDatum(String cWord, Flags flags, Object featuresC, String goldAns) {
//logger.info("Making features for " + cWord + " flags " + flags);
if (flags == null) {
// no features for this column
return;View on GitHub (pinned to 1b7edd19c4)