{"record":{"id":"b80742ed3f37add3","repo":"karatelabs/karate","slug":"nosuchelementexception","errorCode":null,"errorMessage":"NoSuchElementException","messagePattern":"NoSuchElementException","errorType":"exception","errorClass":"java.util.NoSuchElementException","httpStatus":null,"severity":"warning","filePath":"karate-js/src/main/java/io/karatelabs/js/IterUtils.java","lineNumber":509,"sourceCode":"\n    private static final Object[] EMPTY_ARGS = new Object[0];\n\n    /**\n     * Yields nothing; IteratorClose stays the default no-op. Returned when\n     * iterator acquisition itself completed abruptly (the cooperative\n     * stop-flag is set), so callers unwind without further calls into user\n     * code and the pending error propagates unmodified.\n     */\n    private static JsIterator exhaustedIterator() {\n        return new JsIterator() {\n            @Override\n            public boolean hasNext() {\n                return false;\n            }\n\n            @Override\n            public Object next() {\n                throw new NoSuchElementException();\n            }\n        };\n    }\n\n    private static boolean isJsErrored(Context context) {\n        return context instanceof CoreContext cc && cc.isError();\n    }\n\n    /** Reads a slot, invoking an accessor getter if present (so user iterators with\n     *  `get value() { ... }` semantics — common in spec tests — surface their getter\n     *  errors at iteration time). Routes through the receiver-aware\n     *  {@link ObjectLike#getMember(String, Object, CoreContext)}; a non-CoreContext\n     *  context yields {@code undefined} for accessor descriptors (no thread for\n     *  thisObject swap). */\n    /** True when {@code name} exists anywhere on {@code obj}'s prototype chain. */\n    private static boolean hasProperty(ObjectLike obj, String name) {\n        for (ObjectLike o = obj; o != null; o = o.getPrototype()) {\n            if (o.isOwnProperty(name)) {","sourceCodeStart":491,"sourceCodeEnd":527,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/IterUtils.java#L491-L527","documentation":"IterUtils returns a degenerate iterator for empty/unusable iterables whose next() unconditionally throws java.util.NoSuchElementException. hasNext() always returns false, so hitting this means code called next() without consulting hasNext() — the iterator is only safe when the empty check is honored.","triggerScenarios":"Calling next() on the sentinel empty iterator returned by IterUtils (e.g. iterating a null/empty collection, or JS for-of/Iterator protocol consumers that call next() without a proper done result).","commonSituations":"JS loops over an empty Java collection converted via IterUtils; custom iteration code that assumes at least one element; a JS engine calling next() on an exhausted/empty iterator with a protocol mismatch.","solutions":["Check hasNext() before every next() call","Verify the source collection — if it should not be empty, fix why it is empty upstream","In JS, iterate with for...of or Array/Map helpers instead of manual next() calls","If wrapping IterUtils output, return done results instead of throwing"],"exampleFix":"// before\nIterator<?> it = IterUtils.iterator(emptyList);\nObject v = it.next(); // NoSuchElementException\n// after\nIterator<?> it = IterUtils.iterator(emptyList);\nObject v = it.hasNext() ? it.next() : null;","handlingStrategy":"type-guard","validationCode":"if (list == null || list.isEmpty()) return; // skip iteration","typeGuard":"boolean hasNextSafe(Iterator<?> it) { return it != null && it.hasNext(); }","tryCatchPattern":"try {\n  value = it.next();\n} catch (NoSuchElementException e) {\n  value = null; // iterator exhausted or empty sentinel\n}","preventionTips":["Always call hasNext() before next()","Prefer for-each / for...of over manual iterator stepping","Validate collections are non-empty when at least one element is required"],"tags":["javascript","iterator","collections","empty"],"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"}