{"record":{"id":"4278754cf5ac340e","repo":"json-path/JsonPath","slug":"length-operation-can-not-applied-to-null","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/Jackson3JsonNodeJsonProvider.java","lineNumber":217,"sourceCode":"\n    @Override\n    public Collection<String> getPropertyKeys(Object obj) {\n        return toJsonObject(obj).propertyNames();\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 StringNode) {\n                StringNode element = (StringNode) obj;\n                return element.size();\n            }\n        }\n        throw new JsonPathException(\"length operation can not applied to \" + (obj != null ? obj.getClass().getName() : \"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() {\n                        return unwrap(iterator.next());","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/json/Jackson3JsonNodeJsonProvider.java#L199-L235","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nint n = JsonPath.read(json, \"$.items.length()\"); // throws when items is null\n// after\nObject items = JsonPath.read(json, \"$.items\");\nint n = (items instanceof List) ? ((List<?>) items).size() : 0;","handlingStrategy":"type-guard","validationCode":"Object v = JsonPath.read(json, \"$.items\");\nif (v == null) throw new IllegalStateException(\"$.items is null; cannot take length\");","typeGuard":"boolean hasLength(Object o) { return o != null && (o instanceof List || o instanceof Map || o instanceof String); }","tryCatchPattern":"try {\n    return JsonPath.<Integer>read(json, \"$.items.length()\");\n} catch (PathNotFoundException | JsonPathException e) {\n    return 0;\n}","preventionTips":["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"],"tags":["json-path","jackson","length","null"],"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"}