{"record":{"id":"b8da8fc21b6fc19b","repo":"apache/hadoop","slug":"there-are-no-more-elements","errorCode":null,"errorMessage":"There are no more elements","messagePattern":"There are no more elements","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightGSet.java","lineNumber":339,"sourceCode":"        return;\n      }\n      next = cur.getNext();\n      if (next == null) {\n        next = nextNonemptyEntry();\n      }\n    }\n\n    @Override\n    public boolean hasNext() {\n      ensureNext();\n      return next != null;\n    }\n\n    @Override\n    public E next() {\n      ensureNext();\n      if (next == null) {\n        throw new IllegalStateException(\"There are no more elements\");\n      }\n      cur = next;\n      next = null;\n      return convert(cur);\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    @Override\n    public void remove() {\n      ensureNext();\n      if (cur == null) {\n        throw new IllegalStateException(\"There is no current element \" +\n            \"to remove\");\n      }\n      LightWeightGSet.this.remove((K)cur);\n      iterModification++;\n      cur = null;\n    }","sourceCodeStart":321,"sourceCodeEnd":357,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightGSet.java#L321-L357","documentation":"The GSet iterator's next() throws IllegalStateException(\"There are no more elements\") when called with no elements left. This differs from java.util.Iterator, which throws NoSuchElementException - so catch blocks written for the JDK type silently miss this one. hasNext() must gate every next().","triggerScenarios":"Calling next() after the iterator is exhausted, or calling next() repeatedly without checking hasNext() in between.","commonSituations":"Hand-rolled loops migrated from other collections; code that conditionally consumed one element and then calls next() unconditionally; catch (NoSuchElementException) clauses that do not match this exception type.","solutions":["Rewrite the loop as while (it.hasNext()) { E e = it.next(); ... }.","If over-advancement must be tolerated, catch IllegalStateException - but the loop logic is the real bug.","When porting code from java.util collections, remember GSet's next() uses IllegalStateException, not NoSuchElementException."],"exampleFix":"// before\nE e = it.next(); // may be past the end\n\n// after\nwhile (it.hasNext()) {\n  E e = it.next();\n  ...\n}","handlingStrategy":"validation","validationCode":"while (it.hasNext()) {\n  E e = it.next(); // safe: guarded by hasNext()\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always gate next() with hasNext(); never assume a count.","Do not copy catch (NoSuchElementException) handlers to GSet iterators - they throw IllegalStateException."],"tags":["collections","iterator","hadoop-common"],"backgroundTag":"iterator-exhausted","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}