{"record":{"id":"543ac28d19c997e2","repo":"TheAlgorithms/Java","slug":"input-array-cannot-be-null-543ac2","errorCode":null,"errorMessage":"Input array cannot be null","messagePattern":"Input array cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/prefixsum/RangeSumQuery.java","lineNumber":43,"sourceCode":" *\n * @author Ruturaj Jadhav, <a href=\"https://github.com/ruturajjadhav07\">ruturajjadhav07</a>\n */\npublic final class RangeSumQuery {\n\n    private RangeSumQuery() {\n        // Utility class; prevent instantiation\n    }\n\n    /**\n     * Computes the prefix sum array for efficient range queries.\n     *\n     * @param nums The input integer array.\n     * @return Prefix sum array where prefixSum[i+1] = sum of nums[0..i].\n     * @throws IllegalArgumentException if nums is null.\n     */\n    public static int[] buildPrefixSum(int[] nums) {\n        if (nums == null) {\n            throw new IllegalArgumentException(\"Input array cannot be null\");\n        }\n\n        int n = nums.length;\n        int[] prefixSum = new int[n + 1];\n        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].","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/prefixsum/RangeSumQuery.java#L25-L61","documentation":"Thrown by RangeSumQuery.buildPrefixSum(nums) when nums is null. Unlike PrefixSum, this is a static utility that returns a fresh prefix-sum array; null input has no length to iterate. Empty arrays (length 0) are allowed and yield a size-1 prefix array.","triggerScenarios":"RangeSumQuery.buildPrefixSum(null), or buildPrefixSum(someArray) where someArray was never assigned.","commonSituations":"Passing a possibly-null field from a record/DTO; chaining a method that returns null on failure into buildPrefixSum; missing JSON field deserializing to null.","solutions":["Null-check at the caller; pass new int[0] if emptiness is acceptable.","Make the producer return an empty array instead of null (null-object pattern).","Wrap with Objects.requireNonNull(nums, \"nums\") for a clearer NPE if null is truly a bug."],"exampleFix":"// before\nint[] ps = RangeSumQuery.buildPrefixSum(data); // data may be null\n\n// after\nint[] safe = data == null ? new int[0] : data;\nint[] ps = RangeSumQuery.buildPrefixSum(safe);","handlingStrategy":"validation","validationCode":"int[] safe = nums == null ? new int[0] : nums;\nint[] ps = RangeSumQuery.buildPrefixSum(safe);","typeGuard":"static boolean usable(int[] a) {\n    return a != null; // empty allowed\n}","tryCatchPattern":"try {\n    int[] ps = RangeSumQuery.buildPrefixSum(nums);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"Null input to buildPrefixSum\");\n}","preventionTips":["Make data sources return empty arrays, not null.","Coalesce null to empty at the boundary so downstream static utilities never see null.","Wrap with Objects.requireNonNull if null indicates a real bug."],"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"}