{"record":{"id":"f99c95758b42072b","repo":"TheAlgorithms/Java","slug":"input-array-cannot-be-null-or-empty-f99c95","errorCode":null,"errorMessage":"Input array cannot be null or empty.","messagePattern":"Input array cannot be null or empty\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/prefixsum/DifferenceArray.java","lineNumber":36,"sourceCode":" *\n * @see <a href=\"https://en.wikipedia.org/wiki/Finite_difference\">Finite Difference (Wikipedia)</a>\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 DifferenceArray {\n\n    private final long[] differenceArray;\n    private final int n;\n\n    /**\n     * Initializes the Difference Array from a given integer array.\n     *\n     * @param inputArray The initial array. Cannot be null or empty.\n     * @throws IllegalArgumentException if the input array is null or empty.\n     */\n    public DifferenceArray(int[] inputArray) {\n        if (inputArray == null || inputArray.length == 0) {\n            throw new IllegalArgumentException(\"Input array cannot be null or empty.\");\n        }\n        this.n = inputArray.length;\n        // Size n + 1 allows for branchless updates at the right boundary (r + 1).\n        this.differenceArray = new long[n + 1];\n        initializeDifferenceArray(inputArray);\n    }\n\n    private void initializeDifferenceArray(int[] inputArray) {\n        differenceArray[0] = inputArray[0];\n        for (int i = 1; i < n; i++) {\n            differenceArray[i] = inputArray[i] - inputArray[i - 1];\n        }\n    }\n\n    /**\n     * Adds a value to all elements in the range [l, r].\n     *\n     * <p>","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/prefixsum/DifferenceArray.java#L18-L54","documentation":"Thrown by the DifferenceArray constructor when inputArray is null or has length 0. The class needs at least one element to build its internal difference array (size n+1), and dereferences inputArray[0] immediately, so empty/null input has no valid representation.","triggerScenarios":"new DifferenceArray(null), new DifferenceArray(new int[0]), or new DifferenceArray(someList.stream().mapToInt(...).toArray()) when the source collection is empty.","commonSituations":"Reading an array from JSON/CSV that parsed to nothing; a filtered stream yielding zero elements; a query result set that returned no rows before being mapped to int[].","solutions":["Check inputArray != null && inputArray.length > 0 before constructing; handle the empty case at the caller (skip, default, or report).","If emptiness is expected, branch on inputArray.length == 0 and skip DifferenceArray entirely — the structure is meaningless for empty input.","Trace the source producing the array (file parse, DB query) and ensure it yields at least one element."],"exampleFix":"// before\nDifferenceArray da = new DifferenceArray(values); // values may be empty\n\n// after\nif (values == null || values.length == 0) {\n    return; // or throw a domain-specific exception\n}\nDifferenceArray da = new DifferenceArray(values);","handlingStrategy":"validation","validationCode":"if (inputArray == null || inputArray.length == 0) {\n    throw new IllegalArgumentException(\"inputArray must be non-null and non-empty\");\n}\nDifferenceArray da = new DifferenceArray(inputArray);","typeGuard":"static boolean usableForDifferenceArray(int[] a) {\n    return a != null && a.length > 0;\n}","tryCatchPattern":"try {\n    DifferenceArray da = new DifferenceArray(inputArray);\n} catch (IllegalArgumentException e) {\n    // empty input is a domain condition, not a bug — handle by skipping the update phase\n    return Collections.emptyList();\n}","preventionTips":["Validate the data source (file/query) produces at least one element before building the structure.","Return empty collections from producers instead of null or empty arrays when downstream needs data.","DifferenceArray is meaningless for empty input — branch on emptiness at the caller."],"tags":["prefix-sum","input-validation","illegal-argument","data-structure"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}