{"record":{"id":"32daa767ca4fc921","repo":"stanfordnlp/CoreNLP","slug":"error-attempt-to-operate-with-key-of-wrong-leng","errorCode":null,"errorMessage":"Error -- attempt to operate with key of wrong length. depth=depth","messagePattern":"Error -- attempt to operate with key of wrong length\\. depth=depth","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/GeneralizedCounter.java","lineNumber":595,"sourceCode":"    }\n    return next.map.containsKey(key.get(key.size()-1));\n  }\n\n  public GeneralizedCounter<K> reverseKeys() {\n    GeneralizedCounter<K> result = new GeneralizedCounter<>();\n    Set<Map.Entry<List<K>,Double>> entries = entrySet();\n    for (Map.Entry<List<K>,Double> entry: entries) {\n      List<K> list = entry.getKey();\n      double count = entry.getValue();\n      Collections.reverse(list);\n      result.incrementCount(list, count);\n    }\n    return result;\n  }\n\n\n  private void wrongDepth() {\n    throw new RuntimeException(\"Error -- attempt to operate with key of wrong length. depth=\" + depth);\n  }\n\n\n  /**\n   * Returns a read-only synchronous view (not a snapshot) of\n   * {@code this} as a {@link ClassicCounter}.  Any calls to\n   * count-changing or entry-removing operations will result in an\n   * {@link UnsupportedOperationException}.  At some point in the\n   * future, this view may gain limited writable functionality.\n   */\n  public ClassicCounter<List<K>> counterView() {\n    return new CounterView();\n  }\n\n  private class CounterView extends ClassicCounter<List<K>> {\n\n    /**\n     *","sourceCodeStart":577,"sourceCodeEnd":613,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/GeneralizedCounter.java#L577-L613","documentation":"GeneralizedCounter tracks keys of a fixed depth; helper wrongDepth() throws when an operation is invoked with a key whose array length does not equal the counter's depth. The library throws this instead of silently returning 0 or misindexing the nested maps, because key length defines which nesting level is accessed.","triggerScenarios":"Calling getCount/getCounts/incrementCount/incrementCount2D/incrementCount3D/incrementCount1D with an Object[] or E[] key whose length != the depth the GeneralizedCounter was constructed with (e.g. a 2-key array on a depth-3 counter, or a depth-1 key on incrementCount3D).","commonSituations":"Mixing counters of different arity in one code path; building keys dynamically with variable-length lists (String.split results, n-gram windows of varying size); calling incrementCount1D/2D/3D helpers with keys of the wrong arity after changing the depth constructor argument.","solutions":["Check the key array length against counter.depth() (or the constructor argument) before the call and reject/trim/pad keys to the exact depth","Construct the GeneralizedCounter with the depth that matches your actual key arity","Use the variable-depth getTotalCount()/incrementCount overloads or a ClassicCounter if key lengths genuinely vary","Log the offending key and its length next to the expected depth to find where malformed keys are produced"],"exampleFix":"// before\nString[] key = tokens; // variable length\ncounter.incrementCount(key, 1.0); // throws if key.length != depth\n// after\nif (key.length == counter.depth()) {\n  counter.incrementCount(key, 1.0);\n} else {\n  throw new IllegalArgumentException(\"key depth \" + key.length + \" != \" + counter.depth());\n}","handlingStrategy":"validation","validationCode":"if (key == null || key.length != gc.depth()) {\n  throw new IllegalArgumentException(\"key length \" + (key==null?-1:key.length) + \" != depth \" + gc.depth());\n}","typeGuard":"boolean isCorrectDepth(E[] key, GeneralizedCounter<E> gc) { return key != null && key.length == gc.depth(); }","tryCatchPattern":"try {\n  gc.incrementCount(key, 1.0);\n} catch (RuntimeException e) {\n  if (e.getMessage() != null && e.getMessage().startsWith(\"Error -- attempt to operate with key of wrong length\")) {\n    throw new IllegalArgumentException(\"malformed key: \" + Arrays.toString(key), e);\n  }\n  throw e;\n}","preventionTips":["Always build keys with the same length as the counter depth","Use the 1D/2D/3D helper methods that match your key arity","Assert key.length == depth() in test fixtures"],"tags":["java","argument-validation","data-structure"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}