{"record":{"id":"97e64c47a0e1261a","repo":"TheAlgorithms/Java","slug":"input-array-cannot-be-null-97e64c","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/PrefixSum.java","lineNumber":28,"sourceCode":" * <p>This implementation uses a long array for the prefix sums to prevent\n * integer overflow when the sum of elements exceeds Integer.MAX_VALUE.\n *\n * @see <a href=\"https://en.wikipedia.org/wiki/Prefix_sum\">Prefix Sum (Wikipedia)</a>\n * @author Chahat Sandhu, <a href=\"https://github.com/singhc7\">singhc7</a>\n */\npublic class PrefixSum {\n\n    private final long[] prefixSums;\n\n    /**\n     * Constructor to preprocess the input array.\n     *\n     * @param array The input integer array.\n     * @throws IllegalArgumentException if the array is null.\n     */\n    public PrefixSum(int[] array) {\n        if (array == null) {\n            throw new IllegalArgumentException(\"Input array cannot be null\");\n        }\n        this.prefixSums = new long[array.length + 1];\n        this.prefixSums[0] = 0;\n\n        for (int i = 0; i < array.length; i++) {\n            // Automatically promotes int to long during addition\n            this.prefixSums[i + 1] = this.prefixSums[i] + array[i];\n        }\n    }\n\n    /**\n     * Calculates the sum of elements in the range [left, right].\n     * Indices are 0-based.\n     *\n     * @param left  The starting index (inclusive).\n     * @param right The ending index (inclusive).\n     * @return The sum of elements from index left to right as a long.\n     * @throws IndexOutOfBoundsException if indices are out of valid range.","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/prefixsum/PrefixSum.java#L10-L46","documentation":"Thrown by the PrefixSum constructor when the input array is null. An empty array (length 0) is accepted here — it just builds a prefixSums of size 1 — so only null is rejected, differing from DifferenceArray which also rejects empty.","triggerScenarios":"new PrefixSum(null), or new PrefixSum(someField) where someField was never assigned.","commonSituations":"A deserialized object whose int[] field stayed null; a method returning null on failure being passed straight in; null propagated from an upstream JSON parse of a missing field.","solutions":["Null-check the array at the caller and pass Collections.empty / a 0-length array or skip building if null.","Ensure the producer never returns null — return new int[0] as a null-object instead.","Use Optional or a requireNonNull wrapper before construction."],"exampleFix":"// before\nPrefixSum ps = new PrefixSum(arr); // arr may be null\n\n// after\nint[] safe = arr == null ? new int[0] : arr;\nPrefixSum ps = new PrefixSum(safe);","handlingStrategy":"validation","validationCode":"int[] safe = array == null ? new int[0] : array;\nPrefixSum ps = new PrefixSum(safe);","typeGuard":"static boolean usableForPrefixSum(int[] a) {\n    return a != null; // empty is allowed by PrefixSum\n}","tryCatchPattern":"try {\n    PrefixSum ps = new PrefixSum(array);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"Null array passed to PrefixSum\");\n}","preventionTips":["Adopt the null-object pattern: producers return new int[0], never null.","Centralize array construction so a single factory guarantees non-null.","Note PrefixSum allows empty but DifferenceArray does not — don't assume uniform validation across classes."],"tags":["prefix-sum","input-validation","illegal-argument","null-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}