{"record":{"id":"a0947a9c6c278095","repo":"pinpoint-apm/pinpoint","slug":"nosuchelementexception-a0947a","errorCode":null,"errorMessage":"NoSuchElementException","messagePattern":"NoSuchElementException","errorType":"exception","errorClass":"java.util.NoSuchElementException","httpStatus":null,"severity":"error","filePath":"commons-profiler/src/main/java/com/navercorp/pinpoint/common/profiler/concurrent/jsr166/ConcurrentWeakHashMap.java","lineNumber":1197,"sourceCode":"                    }\n                }\n            }\n        }\n\n        public boolean hasNext() {\n            while (nextEntry != null) {\n                if (nextEntry.keyRef.get() != null)\n                    return true;\n                advance();\n            }\n\n            return false;\n        }\n\n        HashEntry<K,V> nextEntry() {\n            do {\n                if (nextEntry == null)\n                    throw new NoSuchElementException();\n\n                lastReturned = nextEntry;\n                currentKey = lastReturned.keyRef.get();\n                advance();\n            } while (currentKey == null); // Skip GC'd keys\n\n            return lastReturned;\n        }\n\n        public void remove() {\n            if (lastReturned == null)\n                throw new IllegalStateException();\n            ConcurrentWeakHashMap.this.remove(currentKey);\n            lastReturned = null;\n        }\n    }\n\n    final class KeyIterator","sourceCodeStart":1179,"sourceCodeEnd":1215,"githubUrl":"https://github.com/pinpoint-apm/pinpoint/blob/744c3d3075e595656abb1ae331ad2c0e4c9eb996/commons-profiler/src/main/java/com/navercorp/pinpoint/common/profiler/concurrent/jsr166/ConcurrentWeakHashMap.java#L1179-L1215","documentation":"The internal HashIterator's nextEntry() throws NoSuchElementException when nextEntry is null — i.e. next() was called after the iterator was exhausted (or all remaining keys were GC'd). Iterators of this weak map are fail-fast per-call, not per-next: you must check hasNext().","triggerScenarios":"Calling iterator.next() (or a for-each that manually drives next()) after hasNext() returned false; iterating while GC collects the weakly-referenced keys, exhausting the iterator sooner than expected.","commonSituations":"Looping map.keySet().iterator().next() assuming at least one element; caching an iterator across awaits/interruptions; weak entries collected between hasNext() and next().","solutions":["Always gate next() with hasNext() (or use the for-each/entrySet loop)","Handle the empty-map case before calling next() directly","Since keys can be GC'd, treat a short iteration as normal and re-check size() rather than caching iteration results"],"exampleFix":"// before\nIterator<K> it = map.keySet().iterator();\nK k = it.next(); // throws when empty\n// after\nIterator<K> it = map.keySet().iterator();\nif (it.hasNext()) { K k = it.next(); }","handlingStrategy":"try-catch","validationCode":"if (!map.isEmpty() && it.hasNext()) { K k = it.next(); }","typeGuard":null,"tryCatchPattern":"try { K k = it.next(); } catch (NoSuchElementException e) { /* iterator exhausted (or keys GC'd): stop */ }","preventionTips":["Always iterate with hasNext() or for-each, never bare next()","Expect iterators of weak maps to shrink as GC collects keys — do not assume stable size","Do not cache iterators across long waits"],"tags":["java","concurrency","iterator","nosuchelement"],"backgroundTag":"iterator-exhausted","analyzedSha":"744c3d3075e595656abb1ae331ad2c0e4c9eb996","analyzedAt":"2026-09-07T18:48:45.289Z","contentChangedAt":"2026-09-07T18:48:45.289Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}