{"record":{"id":"c3271260473342ce","repo":"json-path/JsonPath","slug":"aggregation-function-attempted-to-calculate-value-c32712","errorCode":null,"errorMessage":"Aggregation function attempted to calculate value using empty array","messagePattern":"Aggregation function attempted to calculate value using empty array","errorType":"exception","errorClass":"JsonPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/function/sequence/AbstractSequenceAggregation.java","lineNumber":40,"sourceCode":"    public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {\n        if(ctx.configuration().jsonProvider().isArray(model)){\n\n            Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model);\n            List<Object> objectList = new ArrayList<>();\n            objects.forEach(objectList::add);\n            int targetIndex = this.targetIndex(ctx, parameters);\n            if (targetIndex >= 0) {\n                return objectList.get(targetIndex);\n            } else {\n                int realIndex = objectList.size() + targetIndex;\n                if (realIndex > 0) {\n                    return objectList.get(realIndex);\n                } else {\n                    throw new JsonPathException(\"Target index:\" + targetIndex + \" larger than object count:\" + objectList.size());\n                }\n            }\n        }\n        throw new JsonPathException(\"Aggregation function attempted to calculate value using empty array\");\n    }\n    \n    protected int getIndexFromParameters(EvaluationContext ctx, List<Parameter> parameters) {\n        List<Number> numbers = Parameter.toList(Number.class, ctx, parameters);\n        return numbers.get(0).intValue();\n    }\n}\n","sourceCodeStart":22,"sourceCodeEnd":48,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/function/sequence/AbstractSequenceAggregation.java#L22-L48","documentation":"Same invoke() flow as the sequence functions' index error: if the object list is empty, there is nothing to select and the function throws JsonPathException('Aggregation function attempted to calculate value using empty array'). This branch fires before any index math when objectList has no elements.","triggerScenarios":"Calling first(), last(), index(n), or slice() on an empty array, e.g. $.items.first() where items is [], via JsonPath.read()/compile().","commonSituations":"Taking 'the first' element of collections that can legitimately be empty (no search results, no notifications), and empty fixture data in tests.","solutions":["Check the array is non-empty before invoking first()/last()/index().","Catch JsonPathException and return null/default instead of the first element.","Prefer application-side access (list.get(0)) with an isEmpty() guard for clearer control flow.","Use Option.SUPPRESS_EXCEPTIONS where the library supports suppressing such errors."],"exampleFix":"// before\nObject first = JsonPath.read(json, \"$.results.first()\"); // results = []\n// after\nList<Object> r = JsonPath.read(json, \"$.results\");\nObject first = (r != null && !r.isEmpty()) ? r.get(0) : null;","handlingStrategy":"validation","validationCode":"List<?> r = JsonPath.read(json, \"$.results\");\nObject first = (r != null && !r.isEmpty()) ? r.get(0) : null;","typeGuard":null,"tryCatchPattern":"try {\n    return JsonPath.read(json, \"$.results.first()\");\n} catch (JsonPathException e) {\n    if (e.getMessage().contains(\"empty array\")) return null;\n    throw e;\n}","preventionTips":["Guard first()/last() calls with isEmpty() checks","Default to null rather than throwing when collections may be empty","Use SUPPRESS_EXCEPTIONS where acceptable and null-check results"],"tags":["json-path","aggregation","empty-collection"],"backgroundTag":"empty-result-set","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"}