{"record":{"id":"062634de2c71c20f","repo":"didi/DoKit","slug":"entry-does-not-exist-index","errorCode":null,"errorMessage":"Entry does not exist: \" + index","messagePattern":"Entry does not exist: \" \\+ index","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/CollectionUtils.java","lineNumber":730,"sourceCode":"        if (object instanceof Map) {\n            Map map = (Map) object;\n            Iterator iterator = map.entrySet().iterator();\n            return get(iterator, index);\n        } else if (object instanceof List) {\n            return ((List) object).get(index);\n        } else if (object instanceof Object[]) {\n            return ((Object[]) object)[index];\n        } else if (object instanceof Iterator) {\n            Iterator it = (Iterator) object;\n            while (it.hasNext()) {\n                index--;\n                if (index == -1) {\n                    return it.next();\n                } else {\n                    it.next();\n                }\n            }\n            throw new IndexOutOfBoundsException(\"Entry does not exist: \" + index);\n        } else if (object instanceof Collection) {\n            Iterator iterator = ((Collection) object).iterator();\n            return get(iterator, index);\n        } else if (object instanceof Enumeration) {\n            Enumeration it = (Enumeration) object;\n            while (it.hasMoreElements()) {\n                index--;\n                if (index == -1) {\n                    return it.nextElement();\n                } else {\n                    it.nextElement();\n                }\n            }\n            throw new IndexOutOfBoundsException(\"Entry does not exist: \" + index);\n        } else {\n            try {\n                return Array.get(object, index);\n            } catch (IllegalArgumentException ex) {","sourceCodeStart":712,"sourceCodeEnd":748,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/CollectionUtils.java#L712-L748","documentation":"When CollectionUtils.get receives an Iterator, it consumes elements decrementing index until it hits -1; if the iterator is exhausted first, the requested entry never existed and IndexOutOfBoundsException('Entry does not exist: <index>') is thrown. Note the message reports the already-decremented index, not the original one. The same guard protects callers that pass a Collection, since that path delegates to the Iterator branch.","triggerScenarios":"Calling get(iterator, n) where n >= remaining element count, including n == 0 on an already-exhausted or empty iterator; passing a Collection with index >= size (it forwards to the Iterator branch and throws the same message).","commonSituations":"Reusing a shared iterator that another consumer already advanced; TOCTOU between checking map.entrySet() size and calling get on its iterator while entries are removed; assuming an Iterator can be randomly accessed repeatedly.","solutions":["For Collections and Maps, prefer size-checking first: index < CollectionUtils.size(object), or cast and use List.get.","Request a fresh iterator() immediately before calling get; never share or reuse iterators.","Treat this as an exhaustive-consumption operation: get(iterator, n) advances the iterator n+1 positions."],"exampleFix":"// before\nIterator<Map.Entry<String,String>> it = map.entrySet().iterator();\n// ... it consumed elsewhere ...\nObject v = CollectionUtils.get(it, 0); // may throw: entry already consumed\n\n// after\nObject v = map.isEmpty() ? null : CollectionUtils.get(map, 0); // fresh iterator inside","handlingStrategy":"validation","validationCode":"if (object instanceof Collection && index < ((Collection<?>) object).size()) {\n    return CollectionUtils.get(object, index);\n}\nreturn null;","typeGuard":null,"tryCatchPattern":"try { v = CollectionUtils.get(it, n); } catch (IndexOutOfBoundsException e) { v = null; }","preventionTips":["Remember get(iterator, n) consumes n+1 elements as a side effect.","Always pass a freshly obtained iterator.","For random access, convert to a List first instead of scanning an Iterator."],"tags":["collections","iterator","index-out-of-bounds"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}