{"record":{"id":"72bde993a64a74bd","repo":"json-path/JsonPath","slug":"length-operation-can-not-applied-to-null-72bde9","errorCode":null,"errorMessage":"length operation can not applied to null","messagePattern":"length operation can not applied to null","errorType":"exception","errorClass":"JsonPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java","lineNumber":225,"sourceCode":"        while (iter.hasNext()){\n            keys.add(iter.next());\n        }\n        return keys;\n    }\n\n    @Override\n    public int length(Object obj) {\n        if (isArray(obj)) {\n            return toJsonArray(obj).size();\n        } else if (isMap(obj)) {\n            return toJsonObject(obj).size();\n        } else {\n            if (obj instanceof TextNode) {\n                TextNode element = (TextNode) obj;\n                return element.size();\n            }\n        }\n        throw new JsonPathException(\"length operation can not applied to \" + (obj != null ? obj.getClass().getName()\n                : \"null\"));\n    }\n\n    @Override\n    public Iterable<?> toIterable(Object obj) {\n        ArrayNode arr = toJsonArray(obj);\n        Iterator<?> iterator = arr.iterator();\n        return new Iterable<Object>() {\n            @Override\n            public Iterator<Object> iterator() {\n                return new Iterator<Object>() {\n                    @Override\n                    public boolean hasNext() {\n                        return iterator.hasNext();\n                    }\n\n                    @Override\n                    public Object next() {","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java#L207-L243","documentation":"JacksonJsonNodeJsonProvider.length throws JsonPathException when the target object is not a type that supports length() (Array, Map, Collection, TextNode). The message \"length operation can not applied to null\" (or a class name) means the JsonPath length() function was evaluated on an object that is null or of an unsupported node type (e.g. IntNode, BooleanNode).","triggerScenarios":"Evaluating a filter or length() function such as $.items.length() where the path resolves to null (missing property), a numeric/boolean/object node, or a non-TextNode scalar instead of an array/map/string/collection.","commonSituations":"Optional JSON fields missing in some documents so the path yields null; expecting a string but receiving a number node; calling length() on an object node instead of an array; defensive length checks in filters over heterogeneous documents.","solutions":["Make the path definite (e.g. $.items[*]) or check the node for null/exists before applying length()","Add a filter guard like ?(@.items != null) or use a default value so length() only runs on present arrays/strings","Verify the JSON shape: if the value is a scalar number, compute length differently or fix the schema/producer","Catch JsonPathException around the length evaluation and treat it as 'length undefined'"],"exampleFix":"// before\nInteger n = JsonPath.read(json, \"$.maybeArray.length()\");\n// after\nList<Object> arr = JsonPath.read(json, \"$.maybeArray\");\nInteger n = (arr == null) ? 0 : arr.size();","handlingStrategy":"validation","validationCode":"Object node = JsonPath.read(json, \"$.items\");\nboolean lengthApplies = node != null\n    && (node instanceof List || node instanceof Map || node instanceof String || node instanceof TextNode);\nif (!lengthApplies) throw new IllegalStateException(\"length() not applicable to: \" + node);","typeGuard":"static boolean supportsLength(Object o) {\n    return o != null && (o instanceof List || o instanceof Map\n        || o instanceof Collection || o instanceof CharSequence\n        || o instanceof com.fasterxml.jackson.databind.node.TextNode);\n}","tryCatchPattern":"try {\n    int n = JsonPath.read(json, \"$.items.length()\");\n} catch (JsonPathException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"length operation can not applied\")) {\n        n = 0; // treat undefined length as empty\n    } else throw e;\n}","preventionTips":["Check the path resolves to a non-null array/map/string before length()","Use filters like ?(@.items != null) for optional fields","Validate document shape against a JSON schema for heterogeneous producers","Prefer reading the array and calling .size() yourself over length() on uncertain paths"],"tags":["json-path","null","length","unsupported-operation"],"backgroundTag":"unsupported-operation","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"}