{"record":{"id":"8112870d775e3e4b","repo":"Blankj/AndroidUtilCode","slug":"index-cannot-be-negative","errorCode":null,"errorMessage":"Index cannot be negative: {}","messagePattern":"Index cannot be negative: (.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/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/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/CollectionUtils.java#L692-L728","documentation":"Thrown by CollectionUtils.get(object, index) when index is negative. The method supports Map, List, Object[], Iterator, Collection, and Enumeration, and rejects any negative index up front (before dispatching on type) since none of those structures has a meaningful negative position.","triggerScenarios":"Calling CollectionUtils.get(obj, index) with index < 0; passing a value returned from a failed indexOf (e.g., -1 meaning 'not found') directly as the index.","commonSituations":"Using the result of indexOf()/List.indexOf() (which returns -1 on miss) as the index without checking; underflow from subtracting offsets; parsing a negative number from external input as an index.","solutions":["Check index >= 0 before calling get; treat negative indices as 'no selection'.","When using indexOf results, guard for -1 / INDEX_NOT_FOUND first.","Clamp user-supplied indices to a non-negative value.","Switch to a Map lookup when keys are not positional."],"exampleFix":"// before\nObject v = CollectionUtils.get(list, list.indexOf(target)); // -1 when absent\n\n// after\nint i = list.indexOf(target);\nObject v = (i >= 0) ? CollectionUtils.get(list, i) : null;","handlingStrategy":"validation","validationCode":"if (index >= 0) {\n    Object v = CollectionUtils.get(object, index);\n} else {\n    // negative index is never valid; treat as absent\n}","typeGuard":null,"tryCatchPattern":"try {\n    v = CollectionUtils.get(object, index);\n} catch (IndexOutOfBoundsException e) {\n    v = null; // negative index -> no value\n}","preventionTips":["Always check index >= 0 before calling get.","Guard indexOf()/List.indexOf() results for -1 before reuse as an index.","Clamp parsed/external indices to a non-negative minimum."],"tags":["collection","index-out-of-bounds","negative-index","collectionutils","java"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}