stanfordnlp/CoreNLP · error · IllegalArgumentException

Index not large enough to name all the array elements!

Error message

Index not large enough to name all the array elements!

What it means

Counters.toCounter(double[], Index<T>) builds a Counter from a dense double array by mapping position i to index.get(i). The library throws this IllegalArgumentException when the Index has fewer entries than the array has elements, because elements beyond the index size could not be named.

Solutions

  1. Grow the Index to cover all array positions (index.add for each missing key) before calling toCounter
  2. Shorten the array to index.size() or slice only the positions the index actually names
  3. Verify the Index used at write time is the same one used at read time (serialize/persist them together)

Example fix

// before
Counter<String> c = Counters.toCounter(counts, smallIndex); // throws
// after
for (String key : allKeys) smallIndex.add(key);
if (smallIndex.size() < counts.length) throw new IllegalStateException("still too small");
Counter<String> c = Counters.toCounter(counts, smallIndex);
Defensive patterns

Strategy: validation

Validate before calling

if (index.size() < counts.length) {
  throw new IllegalArgumentException("index.size()=" + index.size() + " < counts.length=" + counts.length);
}
Counter<T> c = Counters.toCounter(counts, index);

Type guard

boolean safe = index != null && counts != null && index.size() >= counts.length;

Try / catch

try {
  return Counters.toCounter(counts, index);
} catch (IllegalArgumentException e) {
  log.error("Index too small: {} vs {}", index.size(), counts.length);
  return null;
}

Prevention

When it happens

Trigger: Calling Counters.toCounter(counts, index) where counts.length > index.size().

Common situations: Rebuilding a Counter after the Index was truncated, filtered, or rebuilt on a smaller vocabulary; passing an array saved from a model trained with a bigger label set; off-by-one errors when padding arrays.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/398882c3c6467369. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/stats/Counters.java:2235

   * @return Returns the maximum element of c that is within the restriction
   *         Collection
   */
  public static <E> E restrictedArgMax(Counter<E> c, Collection<E> restriction) {
    E maxKey = null;
    double max = Double.NEGATIVE_INFINITY;
    for (E key : restriction) {
      double count = c.getCount(key);
      if (count > max) {
        max = count;
        maxKey = key;
      }
    }
    return maxKey;
  }

  public static <T> Counter<T> toCounter(double[] counts, Index<T> index) {
    if (index.size() < counts.length)
      throw new IllegalArgumentException("Index not large enough to name all the array elements!");
    Counter<T> c = new ClassicCounter<>();
    for (int i = 0; i < counts.length; i++) {
      if (counts[i] != 0.0)
        c.setCount(index.get(i), counts[i]);
    }
    return c;
  }

  /**
   * Turns the given map and index into a counter instance. For each entry in
   * counts, its key is converted to a counter key via lookup in the given
   * index.
   */
  public static <E> Counter<E> toCounter(Map<Integer, ? extends Number> counts, Index<E> index) {

    Counter<E> counter = new ClassicCounter<>();
    for (Map.Entry<Integer, ? extends Number> entry : counts.entrySet()) {
      counter.setCount(index.get(entry.getKey()), entry.getValue().doubleValue());

View on GitHub (pinned to 1b7edd19c4)