{"record":{"id":"54d55dc902381969","repo":"stanfordnlp/CoreNLP","slug":"empty-index","errorCode":null,"errorMessage":"Empty index","messagePattern":"Empty index","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/Counters.java","lineNumber":2276,"sourceCode":"  /**\n   * Convert a counter to an array using a specified key index. Infer the dimension of\n   * the returned vector from the index.\n   */\n  public static <E> double[] asArray(Counter<E> counter, Index<E> index) {\n    return Counters.asArray(counter, index, index.size());\n  }\n\n  /**\n   * Convert a counter to an array using a specified key index. This method does *not* expand\n   * the index, so all keys in the set keys(counter) - keys(index) are not added to the\n   * output array. Also note that if counter is being used as a sparse array, the result\n   * will be a dense array with zero entries.\n   *\n   * @return the values corresponding to the index\n   */\n  public static <E> double[] asArray(Counter<E> counter, Index<E> index, int dimension) {\n    if (index.size() == 0) {\n      throw new IllegalArgumentException(\"Empty index\");\n    }\n    Set<E> keys = counter.keySet();\n    double[] array = new double[dimension];\n    for (E key : keys) {\n      int i = index.indexOf(key);\n      if (i >= 0) {\n        array[i] = counter.getCount(key);\n      }\n    }\n    return array;\n  }\n\n  /**\n   * Convert a counter to an array, the order of the array is random\n   */\n  public static <E> double[] asArray(Counter<E> counter) {\n    Set<E> keys = counter.keySet();\n    double[] array = new double[counter.size()];","sourceCodeStart":2258,"sourceCodeEnd":2294,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/Counters.java#L2258-L2294","documentation":"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.","triggerScenarios":"Calling Counters.asArray(counter, index, dim) with a freshly created (or cleared) Index that has size()==0.","commonSituations":"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.","solutions":["Populate the Index with all counter keys (index.addAll(counter.keySet())) before calling asArray","Ensure the vocabulary/index file is actually loaded and non-empty before conversion","If the counter itself is empty, skip the conversion and return a zero array instead"],"exampleFix":"// before\ndouble[] a = Counters.asArray(counter, new Index<>(), dim); // throws\n// after\nIndex<String> index = new Index<>();\nindex.addAll(counter.keySet());\ndouble[] a = Counters.asArray(counter, index, Math.max(dim, index.size()));","handlingStrategy":"validation","validationCode":"if (index == null || index.size() == 0) {\n  throw new IllegalStateException(\"Index must be populated before asArray\");\n}\ndouble[] arr = Counters.asArray(counter, index, dimension);","typeGuard":"boolean ready = index != null && index.size() > 0;","tryCatchPattern":"try {\n  return Counters.asArray(counter, index, dim);\n} catch (IllegalArgumentException e) {\n  log.warn(\"Empty index, returning zero array\");\n  return new double[dim];\n}","preventionTips":["Load the vocabulary/index before any conversion step","Add all counter keys to the index (index.addAll(counter.keySet())) as a setup step","Log index.size() at pipeline start to catch failed loads early"],"tags":["java","index","counter","empty-collection"],"backgroundTag":"empty-required-field","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"}