stanfordnlp/CoreNLP · error · IllegalArgumentException

different number of keys and values.

Error message

different number of keys and values.

What it means

ArrayMap's constructor ArrayMap(K[] keys, V[] values) builds a parallel-array-backed map from two arrays. Because entries are created pairwise, the two arrays must have identical lengths; the library throws IllegalArgumentException up front when keys.length != values.length to avoid silently dropping or mispairing data.

Solutions

  1. Check keys.length == values.length before constructing the ArrayMap and fix the data source that produced the shorter array.
  2. Build the map via the no-arg constructor and putAll(m) from a real Map, which eliminates the parallel-array length coupling.
  3. If arrays may legitimately differ, pad the shorter array or decide explicitly which trailing pairs to drop before construction.

Example fix

// before
ArrayMap<String,Integer> m = new ArrayMap<>(new String[]{"a","b","c"}, new Integer[]{1,2});
// after
String[] keys = {"a","b","c"};
Integer[] vals = {1,2,3};
if (keys.length != vals.length) throw new IllegalArgumentException("key/value length mismatch");
ArrayMap<String,Integer> m = new ArrayMap<>(keys, vals);
Defensive patterns

Strategy: validation

Validate before calling

if (keys == null || values == null || keys.length != values.length) {
  throw new IllegalArgumentException("keys and values must be non-null and same length");
}

Try / catch

try {
  ArrayMap<K,V> m = new ArrayMap<>(keys, values);
} catch (IllegalArgumentException e) {
  // log pair-length mismatch and fall back to putAll from a Map
}

Prevention

When it happens

Trigger: Calling new ArrayMap<K,V>(keys, values) where the keys array and values array have different lengths (e.g. keys.length == 3 but values.length == 2).

Common situations: Hand-assembling key/value arrays where one list was edited without updating the other; filtering nulls or duplicates out of one array but not its partner; loading keys and values from separate config sources of different sizes.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/util/ArrayMap.java:96

  @SuppressWarnings("unchecked")
  public ArrayMap(int capacity) {
    size = 0;
    this.capacity = capacity;
    entryArray = new Entry[capacity];
  }

  @SuppressWarnings("unchecked")
  public ArrayMap(Map<? extends K, ? extends V> m) {
	  size = 0;
	  capacity = m.size();
	  entryArray = new Entry[m.size()];
	  this.putAll(m);
  }

  @SuppressWarnings("unchecked")
  public ArrayMap(K[] keys, V[] values) {
    if (keys.length!=values.length) throw new IllegalArgumentException("different number of keys and values.");
    size = keys.length;
    capacity = size;
    entryArray = new Entry[size];
    for (int i=0; i<keys.length; i++) {
      entryArray[i] = new Entry(keys[i], values[i]);
    }
  }

  public static <K, V> ArrayMap<K, V> newArrayMap() {
    return new ArrayMap<>();
  }

  public static <K, V> ArrayMap<K, V> newArrayMap(int capacity) {
    return new ArrayMap<>(capacity);
  }

  @Override
  public Set<Map.Entry<K,V>> entrySet() {

View on GitHub (pinned to 1b7edd19c4)