{"record":{"id":"9ff5bba24d97f3bf","repo":"didi/DoKit","slug":"index-cannot-be-negative-index","errorCode":null,"errorMessage":"Index cannot be negative: \" + index","messagePattern":"Index cannot be negative: \" \\+ index","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/CollectionUtils.java","lineNumber":710,"sourceCode":"     * <li> Collection -- the value returned is the <code>index</code>-th object\n     * returned by the collection's default iterator, if there is such an element.</li>\n     * <li> Iterator or Enumeration -- the value returned is the\n     * <code>index</code>-th object in the Iterator/Enumeration, if there\n     * is such an element.  The Iterator/Enumeration is advanced to\n     * <code>index</code> (or to the end, if <code>index</code> exceeds the\n     * number of entries) as a side effect of this method.</li>\n     * </ul>\n     *\n     * @param object the object to get a value from\n     * @param index  the index to get\n     * @return the object at the specified index\n     * @throws IndexOutOfBoundsException if the index is invalid\n     * @throws IllegalArgumentException  if the object type is invalid\n     */\n    public static Object get(Object object, int index) {\n        if (object == null) return null;\n        if (index < 0) {\n            throw new IndexOutOfBoundsException(\"Index cannot be negative: \" + index);\n        }\n        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                }","sourceCodeStart":692,"sourceCodeEnd":728,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/CollectionUtils.java#L692-L728","documentation":"CollectionUtils.get(object, index) rejects any negative index immediately with IndexOutOfBoundsException. The method supports Map, List, Object[], Iterator, Collection, Enumeration and reflective arrays, and the negative check runs before any dispatch, so it applies to all of them. This is a strict argument-validation failure, independent of the container's contents.","triggerScenarios":"Passing index < 0, e.g. CollectionUtils.get(list, position - 1) when position == 0; computing an index with Math.round or integer division that can go negative on empty input.","commonSituations":"RecyclerView/ListView adapter code translating an item position to a collection index; 'previous element' lookups (i - 1) at i == 0; sentinel -1 from indexOf being passed through unchecked.","solutions":["Check index >= 0 before calling, returning null or a default when it is not.","Never feed an indexOf()/find() result straight in; check for -1 (or use ArrayUtils.INDEX_NOT_FOUND) first.","Clamp the index: Math.max(0, Math.min(index, size - 1)) when a boundary element is acceptable."],"exampleFix":"// before\nObject prev = CollectionUtils.get(items, currentPosition - 1); // throws at position 0\n\n// after\nObject prev = currentPosition > 0\n        ? CollectionUtils.get(items, currentPosition - 1)\n        : null;","handlingStrategy":"validation","validationCode":"if (index < 0) return null;\nreturn CollectionUtils.get(object, index);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pass indexOf() results without checking for -1.","Guard i-1 / i+1 computations at loop boundaries.","Unit-test boundary indices 0 and size-1 for any indexed helper."],"tags":["collections","index-out-of-bounds","validation"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}