{"record":{"id":"faf02012179aef9e","repo":"stanfordnlp/CoreNLP","slug":"iterator-is-empty","errorCode":null,"errorMessage":"Iterator is empty!","messagePattern":"Iterator is empty!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/CollectionUtils.java","lineNumber":920,"sourceCode":"   * This should respect the remove() functionality of the constituent iterators.\n   *\n   * @param iterators The iterators to concatenate.\n   * @param <E> The type of the iterators.\n   * @return An iterator consisting of all the component iterators concatenated together in order.\n   */\n  @SafeVarargs\n  public static <E> Iterator<E> concatIterators(final Iterator<E>... iterators) {\n    return new Iterator<E>() {\n      Iterator<E> lastIter = null;\n      List<Iterator<E>> iters = new LinkedList<>(Arrays.asList(iterators));\n      @Override\n      public boolean hasNext() {\n        return !iters.isEmpty() && iters.get(0).hasNext();\n      }\n      @Override\n      public E next() {\n        if (!hasNext()) {\n          throw new IllegalArgumentException(\"Iterator is empty!\");\n        }\n        E next = iters.get(0).next();\n        lastIter = iters.get(0);\n        while (!iters.isEmpty() && !iters.get(0).hasNext()) {\n          iters.remove(0);\n        }\n        return next;\n      }\n      @Override\n      public void remove() {\n        if (lastIter == null) {\n          throw new IllegalStateException(\"Call next() before calling remove()!\");\n        }\n        lastIter.remove();\n      }\n    };\n  }\n","sourceCodeStart":902,"sourceCodeEnd":938,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/CollectionUtils.java#L902-L938","documentation":"The concatenated-iterator returned by CollectionUtils (lazy concatenation of a list of iterators) throws IllegalArgumentException from next() when hasNext() is false — i.e. all underlying iterators are exhausted or the iterator list is empty. It signals a misuse: calling next() without a preceding successful hasNext().","triggerScenarios":"Calling next() on the concatenated iterator after exhaustion; calling next() first in a loop without hasNext(); constructing with an empty iterator list and immediately calling next().","commonSituations":"Custom iteration code that assumes an element exists; concurrent removal from the source iterators; iterating after a terminal operation already consumed everything.","solutions":["Always guard calls with if (it.hasNext()) before next().","Use a standard for-each/while(hasNext()) loop rather than manual next() calls.","Check the iterator list is non-empty before iterating; handle the empty case explicitly.","If iterating may run twice, obtain a fresh concatenated iterator for each pass."],"exampleFix":"// before\nE e = concatIter.next();\n// after\nif (concatIter.hasNext()) { E e = concatIter.next(); } else { /* handle empty */ }","handlingStrategy":"type-guard","validationCode":"if (iterators == null || iterators.isEmpty()) { /* handle empty before creating/using iterator */ }","typeGuard":"boolean nextExists(Iterator<?> it) { return it != null && it.hasNext(); }","tryCatchPattern":"try { E e = it.next(); } catch (IllegalArgumentException e) { /* iterator exhausted; handle empty */ }","preventionTips":["Always gate next() behind hasNext()","Use for-each instead of manual next()","Create a fresh iterator for each traversal pass","Handle the empty-iterator-list case explicitly"],"tags":["java","collections","iterator","empty"],"backgroundTag":"empty-result-set","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"}