json-path/JsonPath · error · JsonPathException
length operation can not applied to null
Error message
length operation can not applied to null
What it means
Jackson3JsonNodeJsonProvider.length returns the size of ArrayNodes/ObjectNodes and StringNode values. When the node is none of these — most commonly null, but also numeric/boolean nodes — the provider falls through to the final throw, reporting 'length operation can not applied to null' (or the class name).
Source
Thrown at json-path/src/main/java/com/jayway/jsonpath/spi/json/Jackson3JsonNodeJsonProvider.java:217
@Override
public Collection<String> getPropertyKeys(Object obj) {
return toJsonObject(obj).propertyNames();
}
@Override
public int length(Object obj) {
if (isArray(obj)) {
return toJsonArray(obj).size();
} else if (isMap(obj)) {
return toJsonObject(obj).size();
} else {
if (obj instanceof StringNode) {
StringNode element = (StringNode) obj;
return element.size();
}
}
throw new JsonPathException("length operation can not applied to " + (obj != null ? obj.getClass().getName() : "null"));
}
@Override
public Iterable<?> toIterable(Object obj) {
ArrayNode arr = toJsonArray(obj);
Iterator<?> iterator = arr.iterator();
return new Iterable<Object>() {
@Override
public Iterator<Object> iterator() {
return new Iterator<Object>() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public Object next() {
return unwrap(iterator.next());View on GitHub (pinned to 62a4c9f0f6)
Solutions
- Verify the node is an array/object/string node before applying length() (node.isArray() || node.isObject() || node.isStringNode())
- Null-check the read result and supply a default size (0) when null
- Adjust the path to target an existing container node
- Use Option.SUPPRESS_EXCEPTIONS or read-with-default helpers for optional fields
Example fix
// before int n = JsonPath.read(json, "$.items.length()"); // throws when items is null // after Object items = JsonPath.read(json, "$.items"); int n = (items instanceof List) ? ((List<?>) items).size() : 0;
Defensive patterns
Strategy: type-guard
Validate before calling
Object v = JsonPath.read(json, "$.items");
if (v == null) throw new IllegalStateException("$.items is null; cannot take length"); Type guard
boolean hasLength(Object o) { return o != null && (o instanceof List || o instanceof Map || o instanceof String); } Try / catch
try {
return JsonPath.<Integer>read(json, "$.items.length()");
} catch (PathNotFoundException | JsonPathException e) {
return 0;
} Prevention
- Null-check optional fields before applying length()
- Prefer read-with-default for possibly-absent fields
- Assert field types in contract tests
- Use SUPPRESS_EXCEPTIONS when absence is expected
When it happens
Trigger: Evaluating $.field.length() where field is a null JSON node or a number/boolean; calling length(obj) directly on a null model; length() paths against missing fields that resolve to null nodes.
Common situations: Counting elements of optional array fields; Jackson3 JsonNode provider configs where absent fields produce NullNode; paths written against an older API shape where the field was always an array.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- length operation can not applied to null
- length operation can not applied to null
- length operation cannot be applied to
- length operation can not applied to
- Not a JSON Node
AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11).
Data as JSON: /api/errors/4278754cf5ac340e.
Report an issue: GitHub.