{"record":{"id":"3eda910dcbd41f47","repo":"karatelabs/karate","slug":"nosuchelementexception-jsarray","errorCode":null,"errorMessage":"NoSuchElementException","messagePattern":"NoSuchElementException","errorType":"exception","errorClass":"java.util.NoSuchElementException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsArray.java","lineNumber":988,"sourceCode":"                    if (parseIndex(s.name) >= 0) continue;\n                    if (\"length\".equals(s.name)) continue;\n                    Object val = ctx != null ? s.read(JsArray.this, ctx)\n                            : (s instanceof DataSlot ds ? ds.value : null);\n                    peeked = new KeyValue(JsArray.this, yieldCount++, s.name, val);\n                    return true;\n                }\n                peeked = null;\n                return false;\n            }\n\n            @Override\n            public boolean hasNext() {\n                return peeked != null || advance();\n            }\n\n            @Override\n            public KeyValue next() {\n                if (peeked == null && !advance()) throw new NoSuchElementException();\n                KeyValue kv = peeked;\n                peeked = null;\n                return kv;\n            }\n        };\n    }\n\n    // =================================================================================================\n    // Helper methods\n    // =================================================================================================\n\n    static JsArray toArray(Map<String, Object> map) {\n        List<Object> list = new ArrayList<>();\n        if (map.containsKey(\"length\")) {\n            Object length = map.get(\"length\");\n            if (length instanceof Number) {\n                int size = ((Number) length).intValue();\n                for (int i = 0; i < size; i++) {","sourceCodeStart":970,"sourceCodeEnd":1006,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsArray.java#L970-L1006","documentation":"JsArray's internal KeyValue iterator throws java.util.NoSuchElementException when next() is called after the array's entries are exhausted. The library's own iteration loops always guard with hasNext(); a raw next() past the end means an iteration protocol bug or misuse of the iterator. In JS semantics, calling an exhausted iterator's next() should return {done:true}, never throw.","triggerScenarios":"Calling next() on the JsArray entry iterator without checking hasNext() after all elements have been consumed; an engine-internal loop or custom host code advancing the iterator more times than the array has entries.","commonSituations":"Custom Java host code iterating a Karate JS array; engine bugs during for...of/entries() enumeration after array mutation; concurrent consumption of the same iterator from two loops.","solutions":["Guard every next() call with hasNext() before consuming.","Do not share one iterator across multiple consumers; obtain a fresh iterator per loop.","If it fires during normal JS for...of, report/check for mid-iteration mutation of the array (splice/length changes) and snapshot before iterating.","Upgrade karate-js — later versions harden iterator bounds."],"exampleFix":"// before\nwhile (true) { KeyValue kv = it.next(); ... }\n// after\nwhile (it.hasNext()) { KeyValue kv = it.next(); ... }","handlingStrategy":"try-catch","validationCode":"if (arr == null || arr.length === 0) return; // skip iteration entirely","typeGuard":"boolean canAdvance(Iterator<?> it) { return it != null && it.hasNext(); }","tryCatchPattern":"try { while (it.hasNext()) { process(it.next()); } } catch (NoSuchElementException e) { log.warn(\"iterator over-advanced\", e); }","preventionTips":["Always gate next() behind hasNext() or use for-each.","Never share an iterator across loops or threads.","Snapshot collections before iterating if they can be mutated.","Treat NoSuchElementException from an iterator as a caller bug, not a data condition."],"tags":["javascript","iterator","nosuchelement"],"backgroundTag":"index-out-of-bounds","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"}