{"record":{"id":"92ac8b39ebda562d","repo":"clojure/clojure","slug":"nosuchelementexception-92ac8b","errorCode":null,"errorMessage":"NoSuchElementException","messagePattern":"NoSuchElementException","errorType":"exception","errorClass":"java.util.NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/jvm/clojure/lang/PersistentList.java","lineNumber":243,"sourceCode":"\t}\n\n\tpublic boolean isEmpty(){\n\t\treturn true;\n\t}\n\n\tpublic boolean contains(Object o){\n\t\treturn false;\n\t}\n\n\tpublic Iterator iterator(){\n\t\treturn new Iterator(){\n\n\t\t\tpublic boolean hasNext(){\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tpublic Object next(){\n\t\t\t\tthrow new NoSuchElementException();\n\t\t\t}\n\n\t\t\tpublic void remove(){\n\t\t\t\tthrow new UnsupportedOperationException();\n\t\t\t}\n\t\t};\n\t}\n\n\tpublic Object[] toArray(){\n\t\treturn RT.EMPTY_ARRAY;\n\t}\n\n\tpublic boolean add(Object o){\n\t\tthrow new UnsupportedOperationException();\n\t}\n\n\tpublic boolean remove(Object o){\n\t\tthrow new UnsupportedOperationException();","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/clojure/clojure/blob/f3b143341d6efc6428b523b2eaa099a6cc99156e/src/jvm/clojure/lang/PersistentList.java#L225-L261","documentation":"EmptyList's iterator is a sentinel: hasNext() is always false, so next() has no element and throws NoSuchElementException per the Iterator contract. Fires only when client code violates the contract by calling next() after hasNext() returned false.","triggerScenarios":"Calling next() on the iterator of '() (PersistentList/EmptyList) without a hasNext() check.","commonSituations":"Java interop loops over an empty list, or generic collection-processing code that assumes non-empty input.","solutions":["Check hasNext() before calling next().","Handle the empty-collection case before iterating.","Prefer for-each loops, which skip iteration entirely for empty collections."],"exampleFix":"// before\nObject first = it.next();\n// after\nObject first = it.hasNext() ? it.next() : null;","handlingStrategy":"type-guard","validationCode":"if (lst.isEmpty()) return;","typeGuard":"Object safeNext(java.util.Iterator<?> it) { return it.hasNext() ? it.next() : null; }","tryCatchPattern":"try { e = it.next(); } catch (java.util.NoSuchElementException ex) { e = null; }","preventionTips":["Gate next() behind hasNext().","Early-return on empty collections.","Prefer for-each loops for empty-safe iteration."],"tags":["clojure","iterator","empty-collection"],"backgroundTag":"empty-result-set","analyzedSha":"f3b143341d6efc6428b523b2eaa099a6cc99156e","analyzedAt":"2026-09-09T12:04:58.961Z","contentChangedAt":"2026-09-09T12:04:58.961Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}