stanfordnlp/CoreNLP · error · IllegalArgumentException

Empty index

Error message

Empty index

What it means

Counters.asArray(Counter<E>, Index<E>, int dimension) converts a Counter to a dense double array using the given Index. It throws this IllegalArgumentException when the Index is empty, since a zero-size index means no key could ever map to an array slot.

Solutions

  1. Populate the Index with all counter keys (index.addAll(counter.keySet())) before calling asArray
  2. Ensure the vocabulary/index file is actually loaded and non-empty before conversion
  3. If the counter itself is empty, skip the conversion and return a zero array instead

Example fix

// before
double[] a = Counters.asArray(counter, new Index<>(), dim); // throws
// after
Index<String> index = new Index<>();
index.addAll(counter.keySet());
double[] a = Counters.asArray(counter, index, Math.max(dim, index.size()));
Defensive patterns

Strategy: validation

Validate before calling

if (index == null || index.size() == 0) {
  throw new IllegalStateException("Index must be populated before asArray");
}
double[] arr = Counters.asArray(counter, index, dimension);

Type guard

boolean ready = index != null && index.size() > 0;

Try / catch

try {
  return Counters.asArray(counter, index, dim);
} catch (IllegalArgumentException e) {
  log.warn("Empty index, returning zero array");
  return new double[dim];
}

Prevention

When it happens

Trigger: Calling Counters.asArray(counter, index, dim) with a freshly created (or cleared) Index that has size()==0.

Common situations: Building the index after loading a model fails, so an empty Index is passed; a vocabulary file was not loaded or was parsed incorrectly; counter keys were never added to the index.

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


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

Appendix: source

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

  /**
   * Convert a counter to an array using a specified key index. Infer the dimension of
   * the returned vector from the index.
   */
  public static <E> double[] asArray(Counter<E> counter, Index<E> index) {
    return Counters.asArray(counter, index, index.size());
  }

  /**
   * Convert a counter to an array using a specified key index. This method does *not* expand
   * the index, so all keys in the set keys(counter) - keys(index) are not added to the
   * output array. Also note that if counter is being used as a sparse array, the result
   * will be a dense array with zero entries.
   *
   * @return the values corresponding to the index
   */
  public static <E> double[] asArray(Counter<E> counter, Index<E> index, int dimension) {
    if (index.size() == 0) {
      throw new IllegalArgumentException("Empty index");
    }
    Set<E> keys = counter.keySet();
    double[] array = new double[dimension];
    for (E key : keys) {
      int i = index.indexOf(key);
      if (i >= 0) {
        array[i] = counter.getCount(key);
      }
    }
    return array;
  }

  /**
   * Convert a counter to an array, the order of the array is random
   */
  public static <E> double[] asArray(Counter<E> counter) {
    Set<E> keys = counter.keySet();
    double[] array = new double[counter.size()];

View on GitHub (pinned to 1b7edd19c4)