elastic/elasticsearch · error · IllegalArgumentException
Attempting to address a non-array-like type [receiverClass]
Error message
Attempting to address a non-array-like type [receiverClass] as an array.
What it means
Def.lookupIndexNormalize returns a MethodHandle that normalizes a bracket index (supports negative offsets). It handles arrays, Maps (noop normalize), and Lists (LIST_INDEX_NORMALIZE). For any other receiverClass — a non-array, non-Map, non-List type — it throws 'Attempting to address a non-array-like type [...] as an array', using receiverClass.getCanonicalName().
Source
Thrown at modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java:733
}
/**
* Returns a method handle to normalize the index into an array. This is what makes lists and arrays stored in {@code def} support
* negative offsets.
* @param receiverClass Class of the array to store the value in
* @return a MethodHandle that accepts the receiver as first argument, the index as second argument, and returns the normalized index
* to use with array loads and array stores
*/
static MethodHandle lookupIndexNormalize(Class<?> receiverClass) {
if (receiverClass.isArray()) {
return ArrayIndexNormalizeHelper.arrayIndexNormalizer(receiverClass);
} else if (Map.class.isAssignableFrom(receiverClass)) {
// noop so that mymap[key] doesn't do funny things with negative keys
return MAP_INDEX_NORMALIZE;
} else if (List.class.isAssignableFrom(receiverClass)) {
return LIST_INDEX_NORMALIZE;
}
throw new IllegalArgumentException(
"Attempting to address a non-array-like type " + "[" + receiverClass.getCanonicalName() + "] as an array."
);
}
/**
* Returns a method handle to do an array store.
* @param receiverClass Class of the array to store the value in
* @return a MethodHandle that accepts the receiver as first argument, the index as second argument,
* and the value to set as 3rd argument. Return value is undefined and should be ignored.
*/
static MethodHandle lookupArrayStore(Class<?> receiverClass) {
if (receiverClass.isArray()) {
return MethodHandles.arrayElementSetter(receiverClass);
} else if (Map.class.isAssignableFrom(receiverClass)) {
// maps allow access like mymap[key]
return MAP_PUT;
} else if (List.class.isAssignableFrom(receiverClass)) {
return LIST_SET;View on GitHub (pinned to db6a809a66)
Solutions
- Confirm the receiver is an array, List, or Map before bracket access.
- For Strings, use whitelisted methods (charAt, substring) instead of indexing.
- Type the variable concretely or guard with an instanceof check before indexing.
- Validate params shape before the script runs.
Example fix
// before def s = 'hello'; return s[0]; // String is not array-like // after return s.charAt(0);
Defensive patterns
Strategy: type-guard
Type guard
// Before bracket-read on a def value, narrow the type
function isArrayLike(v) {
return Array.isArray(v) || v instanceof Map || Array.isArray(v); // JS analogue; in Java: v instanceof List || Map || array
}
// if (!isArrayLike(x)) throw new Error('Cannot index into non-array-like type'); Try / catch
// On 'Attempting to address a non-array-like type', advise the author to use String.charAt/substring or to verify the value is a List/Map/array.
Prevention
- Use whitelisted String methods (charAt, substring) instead of indexing Strings.
- Confirm receiver is array/List/Map before bracket access.
- Static-type or instanceof-guard def values before indexing.
When it happens
Trigger: A Painless script uses bracket index access x[i] on a 'def' variable whose runtime class is not an array, Map, or List — e.g. a String, a number, or a custom object. Negative-index normalization is requested but the type can't support indexing at all.
Common situations: Assuming String supports char indexing (it doesn't in Painless def dispatch). Indexing into a scalar or a wrapper type. Runtime type resolving to an unexpected class.
Related errors
- Attempting to address a non-array type [receiverClass] as an
- dynamic method [{}, {}/{}] not found
- Illegal list shortcut value [{}].
- dynamic getter [{}, {}] not found
- Illegal list shortcut value [name].
AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12).
Data as JSON: /api/errors/5c5c5e71be26d1df.
Report an issue: GitHub.