{"record":{"id":"0cfd14107efdbf99","repo":"json-path/JsonPath","slug":"invalid-array-index","errorCode":null,"errorMessage":"Invalid array index","messagePattern":"Invalid array index","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/json/JakartaJsonProvider.java","lineNumber":359,"sourceCode":"            return Boolean.FALSE;\r\n        case NULL:\r\n            return null;\r\n        default:\r\n            return obj;\r\n        }\r\n    }\r\n\r\n    private Integer toArrayIndex(Object index) {\r\n        try {\r\n        \tif (index instanceof Integer) {\r\n        \t\treturn (Integer) index;\r\n        \t} else if (index instanceof Long) {\r\n        \t\treturn Integer.valueOf(((Long) index).intValue());\r\n        \t} else if (index != null) {\r\n        \t\treturn Integer.valueOf(index.toString());\r\n        \t} else {\r\n        \t\t//return null;\r\n\t\t\t\tthrow new IllegalArgumentException(\"Invalid array index\");\r\n            }\r\n        } catch (NumberFormatException e) {\r\n            throw new JsonPathException(e);\r\n        }\r\n    }\r\n\r\n    private JsonValue wrap(Object obj) {\r\n        if (obj == null) {\r\n            return JsonValue.NULL;\r\n        } else if (obj instanceof JsonArray) {\r\n        \tif (!mutableJson || obj instanceof JsonArrayProxy) {\r\n        \t\treturn (JsonArray) obj;\r\n        \t} else {\r\n        \t\treturn proxyAll((JsonArray) obj);\r\n        \t}\r\n        } else if (obj instanceof JsonObject) {\r\n        \tif (!mutableJson || obj instanceof JsonObjectProxy) {\r\n        \t\treturn (JsonObject) obj;\r","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/json/JakartaJsonProvider.java#L341-L377","documentation":"JakartaJsonProvider.toArrayIndex() normalizes an index argument to Integer for array element access. When the supplied index is null — meaning no usable index was provided by the path evaluation — it throws IllegalArgumentException('Invalid array index'). It is an internal guard reached via setProperty and index when a path component cannot be resolved to a concrete array position.","triggerScenarios":"Calling setProperty/mapProperty on an array element with a null or non-numeric key, e.g. path fragments like '$[x]' where the filter yields no index; using JsonPath.set/put API with an index key that evaluates to null; non-numeric string indexes like '$[abc]' reach the same throw via Integer.valueOf(index.toString()) inside the try block only if NumberFormatException — pure null always throws here.","commonSituations":"Building paths programmatically where a placeholder index was never substituted; JSON documents whose array is shorter than expected so the resolver passes null; typos in inline array indexes when mutating documents with the JsonPath.set API.","solutions":["Ensure the array index passed to JsonPath.set/put (or mapProperty) is a non-null Integer or numeric string","Validate the target array length before mutating: provider.length(array) > index","Catch IllegalArgumentException/JsonPathException around mutations and handle the missing-index case explicitly","Use JakartaJsonProvider.setArrayIndex with a concrete int index rather than path fragments that may resolve to null"],"exampleFix":"// before\npath.set(document, \"$[idx].name\", \"x\"); // idx may be null\n// after\nInteger idx = resolveIndexOrNull();\nif (idx != null && idx < provider.length(JsonPath.parse(document).read(\"$\"))) {\n    provider.setArrayIndex(JsonPath.parse(document).read(\"$\"), idx, provider.wrap(\"x\"));\n}","handlingStrategy":"validation","validationCode":"// Java\nif (index == null) throw new IllegalArgumentException(\"Array index required\");\nif (!(index instanceof Integer) && !(index instanceof Long) && !index.toString().matches(\"-?\\\\d+\"))\n    throw new IllegalArgumentException(\"Index must be numeric: \" + index);","typeGuard":"boolean isValidIndex(Object idx) {\n    return idx instanceof Integer || (idx instanceof Long)\n        || (idx != null && idx.toString().matches(\"-?\\\\d+\"));\n}","tryCatchPattern":"try {\n    provider.setProperty(target, \"[\" + idx + \"]\", value);\n} catch (IllegalArgumentException | JsonPathException e) {\n    // handle missing/invalid index: log and skip or throw domain error\n}","preventionTips":["Never build index path fragments from nullable variables without a null check","Check provider.length(array) before setting an element","Distinguish array indexes from map keys in path construction code","Unit-test document mutations against short arrays"],"tags":["json-path","array-index","illegal-argument","mutation"],"backgroundTag":"invalid-argument-value","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}