{"record":{"id":"35bdcbcca52fc036","repo":"TheAlgorithms/Java","slug":"prefix-sum-array-cannot-be-null","errorCode":null,"errorMessage":"Prefix sum array cannot be null","messagePattern":"Prefix sum array cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/prefixsum/RangeSumQuery.java","lineNumber":66,"sourceCode":"        for (int i = 0; i < n; i++) {\n            prefixSum[i + 1] = prefixSum[i] + nums[i];\n        }\n        return prefixSum;\n    }\n\n    /**\n     * Returns the sum of elements from index left to right (inclusive)\n     * using the provided prefix sum array.\n     *\n     * @param prefixSum The prefix sum array computed using buildPrefixSum.\n     * @param left      The start index (inclusive).\n     * @param right     The end index (inclusive).\n     * @return The sum of elements in the range [left, right].\n     * @throws IllegalArgumentException if indices are invalid.\n     */\n    public static int sumRange(int[] prefixSum, int left, int right) {\n        if (prefixSum == null) {\n            throw new IllegalArgumentException(\"Prefix sum array cannot be null\");\n        }\n        if (left < 0 || right >= prefixSum.length - 1 || left > right) {\n            throw new IllegalArgumentException(\"Invalid range indices\");\n        }\n        return prefixSum[right + 1] - prefixSum[left];\n    }\n}\n","sourceCodeStart":48,"sourceCodeEnd":74,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/prefixsum/RangeSumQuery.java#L48-L74","documentation":"Thrown by RangeSumQuery.sumRange(prefixSum, left, right) when prefixSum is null. This overload takes an externally-built prefix-sum array, so it must guard the reference itself. Note the inconsistent API shape: this null check throws IllegalArgumentException, not NullPointerException.","triggerScenarios":"RangeSumQuery.sumRange(null, 0, 3), or passing a prefix-sum array from a different/older build that may be null.","commonSituations":"Reusing a cached prefix-sum field that was cleared to null; mixing this static API with a PrefixSum instance that exposed null; race condition clearing a shared array.","solutions":["Ensure the prefix-sum array passed is the one returned by buildPrefixSum and is non-null.","Null-check before calling: if (prefixSum == null) re-build or skip.","Treat a null prefix sum as a build error: rebuild from the source array instead of querying."],"exampleFix":"// before\nint s = RangeSumQuery.sumRange(cachedPrefix, left, right); // cachedPrefix may be null\n\n// after\nif (cachedPrefix == null) cachedPrefix = RangeSumQuery.buildPrefixSum(source);\nint s = RangeSumQuery.sumRange(cachedPrefix, left, right);","handlingStrategy":"validation","validationCode":"if (prefixSum == null) {\n    prefixSum = RangeSumQuery.buildPrefixSum(source); // rebuild\n}\nint s = RangeSumQuery.sumRange(prefixSum, left, right);","typeGuard":"static boolean usablePrefix(int[] ps) {\n    return ps != null && ps.length >= 1;\n}","tryCatchPattern":"try {\n    int s = RangeSumQuery.sumRange(prefixSum, left, right);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"Null prefixSum passed to sumRange\");\n}","preventionTips":["Keep the source array and prefix sum together so you can rebuild if the cache is null.","Treat a null prefix sum as a build failure, not a query failure.","Prefer the PrefixSum class (encapsulates state) over the static RangeSumQuery when caching."],"tags":["prefix-sum","input-validation","illegal-argument","null-check","static-utility"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}