{"record":{"id":"e1b3e39ede96e118","repo":"stanfordnlp/CoreNLP","slug":"arraycoremap-keyset-iterator-exhausted","errorCode":null,"errorMessage":"ArrayCoreMap keySet iterator exhausted","messagePattern":"ArrayCoreMap keySet iterator exhausted","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"warning","filePath":"src/edu/stanford/nlp/util/ArrayCoreMap.java","lineNumber":178,"sourceCode":"  public Set<Class<?>> keySet() {\n\n    return new AbstractSet<Class<?>>() {\n      @Override\n      public Iterator<Class<?>> iterator() {\n        return new Iterator<Class<?>>() {\n          private int i; // = 0;\n\n          @Override\n          public boolean hasNext() {\n            return i < size;\n          }\n\n          @Override\n          public Class<?> next() {\n            try {\n              return keys[i++];\n            } catch (ArrayIndexOutOfBoundsException aioobe) {\n              throw new NoSuchElementException(\"ArrayCoreMap keySet iterator exhausted\");\n            }\n          }\n\n          @Override\n          @SuppressWarnings(\"unchecked\")\n          public void remove() {\n            ArrayCoreMap.this.remove((Class) keys[i]);\n          }\n        };\n      }\n\n      @Override\n      public int size() {\n        return size;\n      }\n    };\n  }\n","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/ArrayCoreMap.java#L160-L196","documentation":"ArrayCoreMap's keySet iterator converts an ArrayIndexOutOfBoundsException from indexing past the internal keys array into a NoSuchElementException with this message. It is the standard Iterator contract: calling next() after the last key throws. The message is only a diagnostic rethrow, not a corruption indicator.","triggerScenarios":"Calling next() on the keySet()/keyIterator() of an ArrayCoreMap after hasNext() would return false — i.e. iterating past the end, including concurrent modification that shrinks the map while iterating.","commonSituations":"Manual index-based loops over keys without checking i < size(); nested iteration that removes keys mid-loop; forgetting hasNext() when consuming the iterator directly.","solutions":["Use a for-each loop over coremap.keySet() instead of manual next() calls.","Always check hasNext() before next().","Avoid removing keys from the ArrayCoreMap while iterating over its key set.","If index-based access is needed, bounds-check against size() first."],"exampleFix":"// before\nIterator<Class<?>> it = map.keySet().iterator();\nwhile (true) { process(it.next()); } // throws at end\n// after\nfor (Class<?> key : map.keySet()) { process(key); }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { Class<?> k = it.next(); } catch (NoSuchElementException e) { /* iterator exhausted: normal end-of-iteration signal */ }","preventionTips":["Prefer for-each over manual iterator.next() calls.","Never remove keys from an ArrayCoreMap while iterating its key set.","Always pair next() with hasNext()."],"tags":["iterator","nosuch-element","coremap","bounds"],"backgroundTag":"iterator-exhausted","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"}