{"record":{"id":"1b0b803b7a3ceba5","repo":"ReactiveX/RxJava","slug":"no-more-elements-1b0b80","errorCode":null,"errorMessage":"No more elements","messagePattern":"No more elements","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"warning","filePath":"src/main/java/io/reactivex/rxjava4/internal/operators/observable/BlockingObservableNext.java","lineNumber":122,"sourceCode":"            if (nextNotification.isOnComplete()) {\n                return false;\n            }\n            error = nextNotification.getError();\n            throw ExceptionHelper.wrapOrThrow(error);\n        }\n\n        @Override\n        public T next() {\n            if (error != null) {\n                // If any error has already been thrown, throw it again.\n                throw ExceptionHelper.wrapOrThrow(error);\n            }\n            if (hasNext()) {\n                isNextConsumed = true;\n                return next;\n            }\n            else {\n                throw new NoSuchElementException(\"No more elements\");\n            }\n        }\n\n        @Override\n        public void remove() {\n            throw new UnsupportedOperationException(\"Read only iterator\");\n        }\n    }\n\n    static final class NextObserver<T> extends DisposableObserver<Notification<T>> {\n        private final BlockingQueue<Notification<T>> buf = new ArrayBlockingQueue<>(1);\n        final AtomicInteger waiting = new AtomicInteger();\n\n        @Override\n        public void onComplete() {\n            // ignore\n        }\n","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/ReactiveX/RxJava/blob/a8ab5356143f37a8f1dd6d76c79191bec5fa343b/src/main/java/io/reactivex/rxjava4/internal/operators/observable/BlockingObservableNext.java#L104-L140","documentation":"Thrown by Iterator.next() from Observable.blockingNext() when hasNext() is false because the source completed and no further Notification is available. NoSuchElementException(\"No more elements\") marks the normal terminal state of the blocking next-iterator; calling next() past completion triggers it.","triggerScenarios":"Calling next() after the source completed without a hasNext() guard; upstream that emits fewer items than the consumer's fixed loop expects; Observable.empty() consumed via blockingNext().","commonSituations":"Miscounted expected emissions during blocking iteration; an upstream that completes early because of a filter or take(); off-by-one in an index-driven next() loop.","solutions":["Guard next() with hasNext() and terminate the loop when hasNext() returns false.","Verify expected counts with Observable.test().assertValueCount(n) before relying on blocking iteration.","Catch NoSuchElementException to return a default for legitimately empty sources.","Check Observable.isEmpty().blockingGet() first if a non-empty contract is assumed."],"exampleFix":"// before\nIterator<T> it = observable.blockingNext().iterator();\nT first = it.next(); // throws if source empty\n// after\nIterator<T> it = observable.blockingNext().iterator();\nif (!it.hasNext()) { return defaultValue; }\nT first = it.next();","handlingStrategy":"try-catch","validationCode":"// Pre-check non-empty before consuming the blocking next-iterator.\nboolean empty = observable.isEmpty().blockingGet();\nIterator<T> it = observable.blockingNext().iterator();\nif (!it.hasNext()) { /* source completed empty */ }","typeGuard":null,"tryCatchPattern":"Iterator<T> it = observable.blockingNext().iterator();\nT v;\nif (it.hasNext()) {\n    v = it.next();\n} else {\n    v = defaultValue; // empty completion\n}\n// Defensive guard for a stray next() after completion:\ntry { it.next(); }\ncatch (NoSuchElementException e) { /* terminal */ }","preventionTips":["Guard next() with hasNext() and stop on false.","Use Observable.isEmpty().blockingGet() to pre-check a non-empty contract.","Assert expected emission counts with test() before relying on blocking iteration.","Return a default on NoSuchElementException when empty completion is acceptable."],"tags":["rxjava","nosuchelement","blocking","observable","iterator","terminal"],"backgroundTag":null,"analyzedSha":"a8ab5356143f37a8f1dd6d76c79191bec5fa343b","analyzedAt":"2026-08-13T23:25:30.069Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}