{"record":{"id":"2f1f928e7a259564","repo":"json-path/JsonPath","slug":"length-operation-can-not-applied-to-null-2f1f92","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/JakartaJsonProvider.java","lineNumber":281,"sourceCode":"    public int length(Object obj) {\r\n        if (isArray(obj)) {\r\n            if (obj instanceof JsonArrayBuilder) {\r\n                return ((JsonArrayBuilder) obj).build().size();\r\n            } else {\r\n                return ((List<?>) obj).size();\r\n            }\r\n        } else if (isMap(obj)) {\r\n            if (obj instanceof JsonObjectBuilder) {\r\n                obj = ((JsonObjectBuilder) obj).build();\r\n            }\r\n            return ((JsonObject) obj).size();\r\n        } else {\r\n            if (obj instanceof CharSequence) {\r\n                return ((CharSequence) obj).length();\r\n            }\r\n        }\r\n        String className = obj != null ? obj.getClass().getName() : null;\r\n        throw new JsonPathException(\"length operation can not applied to \" + className);\r\n    }\r\n\r\n    @Override\r\n    public Iterable<?> toIterable(Object obj) {\r\n        List<Object> values;\r\n        if (isArray(obj)) {\r\n            if (obj instanceof JsonArrayBuilder) {\r\n                obj = ((JsonArrayBuilder) obj).build();\r\n            }\r\n            values = new ArrayList<Object>(((List<?>) obj).size());\r\n            for (Object val : ((List<?>) obj)) {\r\n                values.add(unwrap(val));\r\n            }\r\n        } else if (isMap(obj)) {\r\n            if (obj instanceof JsonObjectBuilder) {\r\n                obj = ((JsonObjectBuilder) obj).build();\r\n            }\r\n            values = new ArrayList<Object>(((JsonObject) obj).size());\r","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/json/JakartaJsonProvider.java#L263-L299","documentation":"JakartaJsonProvider.length() computes lengths for arrays, maps, JSON values with arrays, and CharSequences; anything else (numbers, booleans, null) causes JsonPathException \"length operation can not applied to <class>\". It backs JsonPath's .length() function, which is undefined for scalar non-string values.","triggerScenarios":"Using the path function length() (e.g. $.items.length() or $.someNumber.length()) where the resolved node is null, a number, or a boolean.","commonSituations":"Path expressions evaluated against documents where the target is missing (null) or where a field changed type from string/array to number between data versions.","solutions":["Ensure the path expression targets an array, object, or string before applying .length().","Guard with a filter or optional evaluation (JsonPath.using(conf).withListeners...) and check for MISSING/undefined results first.","Verify the JSON schema: if the field can be null, branch your query accordingly.","Catch JsonPathException around length evaluation for defensive handling."],"exampleFix":"// before\nint n = JsonPath.read(doc, \"$.maybeMissing.length()\");\n// after\nObject node = JsonPath.read(doc, \"$.maybeMissing\");\nint n = (node instanceof List || node instanceof CharSequence) ? JsonPath.read(doc, \"$.maybeMissing.length()\") : 0;","handlingStrategy":"validation","validationCode":"// Java\nObject node = JsonPath.read(doc, path);\nboolean lengthSafe = node instanceof List || node instanceof java.util.Map || node instanceof CharSequence;","typeGuard":"// Java\nstatic boolean lengthSupported(Object o) {\n    return o instanceof List || o instanceof java.util.Map || o instanceof CharSequence;\n}","tryCatchPattern":"try { return JsonPath.read(doc, path + \".length()\"); } catch (JsonPathException e) { return 0; }","preventionTips":["Apply .length() only to arrays, objects, or strings","Null-check resolved nodes before length queries","Pin schemas so fields do not change type across versions"],"tags":["json-path","length","type-mismatch","null"],"backgroundTag":"type-mismatch","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"}