{"record":{"id":"1d5ff72e0018b884","repo":"TheAlgorithms/Java","slug":"input-arrays-cannot-be-null","errorCode":null,"errorMessage":"Input arrays cannot be null.","messagePattern":"Input arrays cannot be null\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOne.java","lineNumber":32,"sourceCode":"\n    private KnapsackZeroOne() {\n        // Prevent instantiation\n    }\n\n    /**\n     * Solves the 0/1 Knapsack problem using recursion.\n     *\n     * @param values   the array containing values of the items\n     * @param weights  the array containing weights of the items\n     * @param capacity the total capacity of the knapsack\n     * @param n        the number of items\n     * @return the maximum total value achievable within the given weight limit\n     * @throws IllegalArgumentException if input arrays are null, empty, or\n     *     lengths mismatch\n     */\n    public static int compute(final int[] values, final int[] weights, final int capacity, final int n) {\n        if (values == null || weights == null) {\n            throw new IllegalArgumentException(\"Input arrays cannot be null.\");\n        }\n        if (values.length != weights.length) {\n            throw new IllegalArgumentException(\"Value and weight arrays must be of the same length.\");\n        }\n        if (capacity < 0 || n < 0) {\n            throw new IllegalArgumentException(\"Invalid input: arrays must be non-empty and capacity/n \"\n                + \"non-negative.\");\n        }\n        if (n == 0 || capacity == 0 || values.length == 0) {\n            return 0;\n        }\n\n        if (weights[n - 1] <= capacity) {\n            final int include = values[n - 1] + compute(values, weights, capacity - weights[n - 1], n - 1);\n            final int exclude = compute(values, weights, capacity, n - 1);\n            return Math.max(include, exclude);\n        } else {\n            return compute(values, weights, capacity, n - 1);","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOne.java#L14-L50","documentation":"KnapsackZeroOne.compute uses recursion over n items and indexes into both arrays. It rejects null values or weights arrays upfront with IllegalArgumentException to prevent an NPE during the recursive traversal.","triggerScenarios":"Calling compute(values, weights, capacity, n) where values or weights is null — typically from an uninitialized field, a failed deserialization, or a default null placeholder.","commonSituations":"Optional array parameters that default to null; JSON deserialization where the payload omits the field; test code that passes null intentionally but forgets to guard.","solutions":["Initialize arrays to empty (new int[0]) rather than null as a default.","Add a null check before the call and provide a meaningful default or error.","Use Objects.requireNonNull(values, \"values\") at the call boundary."],"exampleFix":"// before\nint r = KnapsackZeroOne.compute(null, weights, cap, n); // throws\n\n// after\nint[] vals = Objects.requireNonNullElseGet(values, () -> new int[0]);\nint r = KnapsackZeroOne.compute(vals, weights, cap, n);","handlingStrategy":"validation","validationCode":"if (values == null || weights == null) {\n    throw new IllegalArgumentException(\"Arrays must not be null\");\n}\nint r = KnapsackZeroOne.compute(values, weights, capacity, n);","typeGuard":"static boolean areNonNullArrays(int[] values, int[] weights) {\n    return values != null && weights != null;\n}","tryCatchPattern":null,"preventionTips":["Default array fields to new int[0] instead of null.","Use Objects.requireNonNull at the call boundary."],"tags":["knapsack","null-check","input-validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}