{"record":{"id":"398882c3c6467369","repo":"stanfordnlp/CoreNLP","slug":"index-not-large-enough-to-name-all-the-array-eleme","errorCode":null,"errorMessage":"Index not large enough to name all the array elements!","messagePattern":"Index not large enough to name all the array elements!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/Counters.java","lineNumber":2235,"sourceCode":"   * @return Returns the maximum element of c that is within the restriction\n   *         Collection\n   */\n  public static <E> E restrictedArgMax(Counter<E> c, Collection<E> restriction) {\n    E maxKey = null;\n    double max = Double.NEGATIVE_INFINITY;\n    for (E key : restriction) {\n      double count = c.getCount(key);\n      if (count > max) {\n        max = count;\n        maxKey = key;\n      }\n    }\n    return maxKey;\n  }\n\n  public static <T> Counter<T> toCounter(double[] counts, Index<T> index) {\n    if (index.size() < counts.length)\n      throw new IllegalArgumentException(\"Index not large enough to name all the array elements!\");\n    Counter<T> c = new ClassicCounter<>();\n    for (int i = 0; i < counts.length; i++) {\n      if (counts[i] != 0.0)\n        c.setCount(index.get(i), counts[i]);\n    }\n    return c;\n  }\n\n  /**\n   * Turns the given map and index into a counter instance. For each entry in\n   * counts, its key is converted to a counter key via lookup in the given\n   * index.\n   */\n  public static <E> Counter<E> toCounter(Map<Integer, ? extends Number> counts, Index<E> index) {\n\n    Counter<E> counter = new ClassicCounter<>();\n    for (Map.Entry<Integer, ? extends Number> entry : counts.entrySet()) {\n      counter.setCount(index.get(entry.getKey()), entry.getValue().doubleValue());","sourceCodeStart":2217,"sourceCodeEnd":2253,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/Counters.java#L2217-L2253","documentation":"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.","triggerScenarios":"Calling Counters.toCounter(counts, index) where counts.length > index.size().","commonSituations":"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.","solutions":["Grow the Index to cover all array positions (index.add for each missing key) before calling toCounter","Shorten the array to index.size() or slice only the positions the index actually names","Verify the Index used at write time is the same one used at read time (serialize/persist them together)"],"exampleFix":"// before\nCounter<String> c = Counters.toCounter(counts, smallIndex); // throws\n// after\nfor (String key : allKeys) smallIndex.add(key);\nif (smallIndex.size() < counts.length) throw new IllegalStateException(\"still too small\");\nCounter<String> c = Counters.toCounter(counts, smallIndex);","handlingStrategy":"validation","validationCode":"if (index.size() < counts.length) {\n  throw new IllegalArgumentException(\"index.size()=\" + index.size() + \" < counts.length=\" + counts.length);\n}\nCounter<T> c = Counters.toCounter(counts, index);","typeGuard":"boolean safe = index != null && counts != null && index.size() >= counts.length;","tryCatchPattern":"try {\n  return Counters.toCounter(counts, index);\n} catch (IllegalArgumentException e) {\n  log.error(\"Index too small: {} vs {}\", index.size(), counts.length);\n  return null;\n}","preventionTips":["Persist the Index together with the array/model so they are reloaded in sync","Assert index.size() == counts.length in unit tests around model IO","Never rebuild an Index from filtered data when arrays depend on the original order"],"tags":["java","index","counter","argument-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}