{"record":{"id":"4b9553f558a2510a","repo":"TheAlgorithms/Java","slug":"no-more-elements-in-the-bag","errorCode":null,"errorMessage":"No more elements in the bag.","messagePattern":"No more elements in the bag\\.","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/bags/Bag.java","lineNumber":131,"sourceCode":"         * Checks if there are more elements to iterate over.\n         *\n         * @return {@code true} if there are more elements; {@code false} otherwise\n         */\n        @Override\n        public boolean hasNext() {\n            return currentElement != null;\n        }\n\n        /**\n         * Returns the next element in the iteration.\n         *\n         * @return the next element in the bag\n         * @throws NoSuchElementException if there are no more elements to return\n         */\n        @Override\n        public E next() {\n            if (!hasNext()) {\n                throw new NoSuchElementException(\"No more elements in the bag.\");\n            }\n            E element = currentElement.content;\n            currentElement = currentElement.nextElement;\n            return element;\n        }\n    }\n}\n","sourceCodeStart":113,"sourceCodeEnd":139,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/bags/Bag.java#L113-L139","documentation":"Thrown by Bag's iterator next() when hasNext() is false — i.e., the iterator has been fully consumed and there are no more elements. This is the standard contract violation for java.util.Iterator: calling next() after exhaustion.","triggerScenarios":"Calling iterator.next() without guarding with hasNext(); using a raw while(true) loop that doesn't check hasNext(); calling next() on an iterator obtained from an empty bag.","commonSituations":"Manual iteration loops that forget the hasNext check; for-each loops are safe but explicit iterator.next() calls in while loops are error-prone; reusing an exhausted iterator.","solutions":["Always check hasNext() before next() in explicit iterator loops.","Prefer the enhanced for-each loop which handles the check automatically.","For empty bags, short-circuit before iterating."],"exampleFix":"// before\nIterator<E> it = bag.iterator();\nwhile (true) { E e = it.next(); /* process */ }\n// after\nfor (E e : bag) { /* process */ }\n// or explicitly:\nIterator<E> it = bag.iterator();\nwhile (it.hasNext()) { E e = it.next(); /* process */ }","handlingStrategy":"validation","validationCode":"// Always check hasNext before next\nIterator<E> it = bag.iterator();\nwhile (it.hasNext()) {\n    E e = it.next();\n    // process e\n}","typeGuard":null,"tryCatchPattern":"Iterator<E> it = bag.iterator();\ntry {\n    while (true) {\n        E e = it.next();\n        // process\n    }\n} catch (NoSuchElementException e) {\n    // iteration complete\n}","preventionTips":["Prefer enhanced for-each loops over manual iterator.next() calls.","Always guard next() with hasNext().","Never reuse an exhausted iterator; obtain a fresh one from bag.iterator()."],"tags":["java","datastructures","iterator","nosuchelement"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}