{"record":{"id":"4d735bb13e3bdabf","repo":"TheAlgorithms/Java","slug":"values-and-weights-arrays-must-be-non-null-and-of","errorCode":null,"errorMessage":"Values and weights arrays must be non-null and of same length.","messagePattern":"Values and weights arrays must be non-null and of same length\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOneTabulation.java","lineNumber":41,"sourceCode":"    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++) {\n                if (currentWeight <= w) {\n                    final int includeItem = currentValue + dp[i - 1][w - currentWeight];\n                    final int excludeItem = dp[i - 1][w];","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOneTabulation.java#L23-L59","documentation":"KnapsackZeroOneTabulation.compute iterates items in parallel over values and weights, populating dp[i][w]. The two arrays must have the same length to avoid an ArrayIndexOutOfBoundsException. The method rejects a length mismatch with IllegalArgumentException before allocating the DP table.","triggerScenarios":"Passing values and weights arrays of different lengths — from independent data sources, partial updates, or a filter applied to one array but not the other.","commonSituations":"Loading items from two CSV columns that have mismatched row counts; appending an item to values but forgetting weights; deserializing a payload where one array field is truncated.","solutions":["Assert values.length == weights.length before calling compute.","Maintain items as a single collection of (value, weight) pairs and split atomically.","Add a data-integrity check at the ingestion boundary."],"exampleFix":"// before\nint r = KnapsackZeroOneTabulation.compute(values, weights, cap, count); // throws\n\n// after\nassert values.length == weights.length : \"item arrays must match\";\nint r = KnapsackZeroOneTabulation.compute(values, weights, cap, count);","handlingStrategy":"validation","validationCode":"if (values.length != weights.length) {\n    throw new IllegalStateException(\"Values and weights arrays must match in length\");\n}\nint r = KnapsackZeroOneTabulation.compute(values, weights, capacity, itemCount);","typeGuard":"static boolean areMatchingLengths(int[] values, int[] weights) {\n    return values != null && weights != null && values.length == weights.length;\n}","tryCatchPattern":null,"preventionTips":["Store items as (value, weight) pairs in a single collection.","Add a data-integrity check at the ingestion boundary."],"tags":["knapsack","array-mismatch","input-validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}