{"record":{"id":"a59ccf58e82a5d93","repo":"stanfordnlp/CoreNLP","slug":"argument-array-lengths-differ-keys-vs-values","errorCode":null,"errorMessage":"Argument array lengths differ: <keys> vs. <values>","messagePattern":"Argument array lengths differ: <keys> vs\\. <values>","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/CoreLabel.java","lineNumber":177,"sourceCode":"    cl.setValue(word);\n    return cl;\n  }\n\n  /**\n   * Class that all \"generic\" annotations extend.\n   * This allows you to read in arbitrary values from a file as features, for example.\n   */\n  public interface GenericAnnotation<T> extends CoreAnnotation<T> {  }\n\n  public static final Map<String, Class<? extends GenericAnnotation<String>>> genericKeys = Generics.newHashMap();\n\n  public static final Map<Class<? extends GenericAnnotation<String>>, String> genericValues = Generics.newHashMap();\n\n\n  @SuppressWarnings({\"unchecked\", \"rawtypes\"})\n  private void initFromStrings(String[] keys, String[] values) {\n    if (keys.length != values.length) {\n      throw new UnsupportedOperationException(\"Argument array lengths differ: \" +\n              Arrays.toString(keys) + \" vs. \" + Arrays.toString(values));\n    }\n    for (int i = 0; i < keys.length; i++) {\n      String key = keys[i];\n      String value = values[i];\n      Class coreKeyClass = AnnotationLookup.toCoreKey(key);\n\n      //now work with the key we got above\n      if (coreKeyClass == null) {\n        if (key != null) {\n          throw new UnsupportedOperationException(\"Unknown key \" + key);\n        }\n      } else {\n        try {\n          Class<?> valueClass = AnnotationLookup.getValueType(coreKeyClass);\n          if(valueClass.equals(String.class)) {\n            this.set(coreKeyClass, values[i]);\n          } else if(valueClass == Integer.class) {","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/CoreLabel.java#L159-L195","documentation":"CoreLabel.initFromStrings throws UnsupportedOperationException when the keys and values string arrays passed to the constructor have different lengths. The label format requires a 1:1 pairing of annotation keys and values.","triggerScenarios":"Calling new CoreLabel(keys, values) (or the constructor variants that delegate to initFromStrings) with keys.length != values.length, e.g. malformed CoNLL/TSV line splits where a column was dropped or an extra tab added.","commonSituations":"Loading labels from CSV/TSV files with inconsistent column counts; splitting a line on a wrong delimiter so a value containing the delimiter shifts columns; constructing labels programmatically with partially filled arrays.","solutions":["Verify the keys and values arrays come from the same split and have equal length before constructing.","Normalize the input line's delimiter (e.g. split on \\t consistently, trim trailing separators).","Pad or trim one array to match the other only if the format genuinely allows missing columns.","Log both Arrays.toString(keys) and Arrays.toString(values) to find which column is missing/extra."],"exampleFix":"// before\nString[] parts = line.split(\"\\\\t\");\nnew CoreLabel(keys, parts); // lengths may differ\n// after\nString[] parts = line.split(\"\\\\t\", -1);\nif (keys.length != parts.length)\n  throw new IllegalArgumentException(\"Expected \" + keys.length + \" columns, got \" + parts.length + \": \" + line);\nnew CoreLabel(keys, parts);","handlingStrategy":"validation","validationCode":"if (keys == null || values == null || keys.length != values.length) {\n  throw new IllegalArgumentException(\"keys and values must be non-null and equal length\");\n}\nnew CoreLabel(keys, values);","typeGuard":null,"tryCatchPattern":"try {\n  return new CoreLabel(keys, values);\n} catch (UnsupportedOperationException e) {\n  if (e.getMessage().startsWith(\"Argument array lengths differ\")) {\n    throw new IllegalArgumentException(\"Malformed label record: \" + e.getMessage(), e);\n  }\n  throw e;\n}","preventionTips":["Split TSV lines with split(\"\\\\t\", -1) and validate the column count against the header.","Derive keys and values from the same single split operation.","Unit-test the loader with lines containing missing and extra columns."],"tags":["argument-mismatch","corelabel","array-length","parsing"],"backgroundTag":"invalid-argument","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"}