{"record":{"id":"95fae125c977a9c0","repo":"karatelabs/karate","slug":"nosuchelementexception-iterator-exhausted","errorCode":null,"errorMessage":"NoSuchElementException (iterator exhausted)","messagePattern":"NoSuchElementException \\(iterator exhausted\\)","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/match/DiskBackedList.java","lineNumber":270,"sourceCode":"                nextLine = reader.readLine();\n                if (nextLine == null) {\n                    closeReader();\n                    return false;\n                }\n                return true;\n            } catch (IOException e) {\n                closeReader();\n                throw new RuntimeException(\"failed to read next line\", e);\n            }\n        }\n\n        @Override\n        public Object next() {\n            if (!hasNextCalled) {\n                hasNext();\n            }\n            if (nextLine == null) {\n                throw new NoSuchElementException();\n            }\n            hasNextCalled = false;\n            currentIndex++;\n            return deserializeItem(nextLine);\n        }\n\n        private void closeReader() {\n            try {\n                reader.close();\n            } catch (IOException e) {\n                logger.warn(\"failed to close reader: {}\", e.getMessage());\n            }\n        }\n    }\n\n}\n","sourceCodeStart":252,"sourceCodeEnd":287,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/match/DiskBackedList.java#L252-L287","documentation":"DiskBackedIterator.next() throws java.util.NoSuchElementException when nextLine is null, i.e. hasNext() reported exhaustion. This is the standard Iterator contract: calling next() past the end of the list.","triggerScenarios":"Calling next() more times than there are elements; calling next() without checking hasNext() when the flag-based hasNext has already returned false; loops that assume a fixed element count larger than the actual size.","commonSituations":"Off-by-one loops; manual iteration without hasNext checks; assuming the disk-backed list size matches another collection's size.","solutions":["Always guard with hasNext() before next()","Use the enhanced for-loop / stream APIs instead of manual next() calls","Track remaining count if index-based logic must be used","Treat NoSuchElementException as an iteration-bug signal, not a data problem"],"exampleFix":"// before\nwhile (true) { process(it.next()); } // exhausts\n// after\nwhile (it.hasNext()) { process(it.next()); }","handlingStrategy":"try-catch","validationCode":"// hasNext() gate before next()\nif (it.hasNext()) { Object v = it.next(); }","typeGuard":"Object nextOrNull(java.util.Iterator<?> it) { return it.hasNext() ? it.next() : null; }","tryCatchPattern":"try { return it.next(); } catch (NoSuchElementException e) { return null; } // treat as end of iteration","preventionTips":["Always call hasNext() before next()","Prefer for-each loops which cannot over-advance","Do not assume element counts; derive bounds from the iterator itself"],"tags":["iterator","nosuch-element","collections","off-by-one"],"backgroundTag":"empty-result-set","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}