{"record":{"id":"8ff46d370d4d4801","repo":"TheAlgorithms/Java","slug":"values-and-weights-arrays-must-not-be-null","errorCode":null,"errorMessage":"Values and weights arrays must not be null.","messagePattern":"Values and weights arrays must not be null\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOneTabulation.java","lineNumber":38,"sourceCode":" */\npublic final class KnapsackZeroOneTabulation {\n\n    private KnapsackZeroOneTabulation() {\n        // Prevent instantiation\n    }\n\n    /**\n     * Solves the 0-1 Knapsack problem using the bottom-up tabulation technique.\n     * @param values the values of the items\n     * @param weights the weights of the items\n     * @param capacity the total capacity of the knapsack\n     * @param itemCount the number of items\n     * @return the maximum value that can be put in the knapsack\n     * @throws IllegalArgumentException if input arrays are null, of different lengths,or if capacity or itemCount is invalid\n     */\n    public static int compute(final int[] values, final int[] weights, final int capacity, final int itemCount) {\n        if (values == null || weights == null) {\n            throw new IllegalArgumentException(\"Values and weights arrays must not be null.\");\n        }\n        if (values.length != weights.length) {\n            throw new IllegalArgumentException(\"Values and weights arrays must be non-null and of same length.\");\n        }\n        if (capacity < 0) {\n            throw new IllegalArgumentException(\"Capacity must not be negative.\");\n        }\n        if (itemCount < 0 || itemCount > values.length) {\n            throw new IllegalArgumentException(\"Item count must be between 0 and the length of the values array.\");\n        }\n\n        final int[][] dp = new int[itemCount + 1][capacity + 1];\n\n        for (int i = 1; i <= itemCount; i++) {\n            final int currentValue = values[i - 1];\n            final int currentWeight = weights[i - 1];\n\n            for (int w = 1; w <= capacity; w++) {","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOneTabulation.java#L20-L56","documentation":"KnapsackZeroOneTabulation.compute allocates a 2D DP table and indexes both arrays by item index. It rejects null values or weights arrays with IllegalArgumentException to prevent an NPE during table population.","triggerScenarios":"Calling compute(values, weights, capacity, itemCount) where values or weights is null — from uninitialized fields, failed JSON deserialization, or optional parameters left null.","commonSituations":"Deserialized request objects with nullable array fields; test fixtures that omit one array; legacy code using null to mean 'no items' instead of an empty array.","solutions":["Default array fields to new int[0] instead of null.","Guard with Objects.requireNonNull at the call boundary.","If null means 'no items', return 0 early instead of calling compute."],"exampleFix":"// before\nint r = KnapsackZeroOneTabulation.compute(values, null, cap, count); // throws\n\n// after\nint[] w = Objects.requireNonNullElseGet(weights, () -> new int[0]);\nint r = KnapsackZeroOneTabulation.compute(values, w, cap, Math.min(count, w.length));","handlingStrategy":"validation","validationCode":"if (values == null || weights == null) {\n    throw new IllegalArgumentException(\"Arrays must not be null\");\n}\nint r = KnapsackZeroOneTabulation.compute(values, weights, capacity, itemCount);","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"}