TheAlgorithms/Java · error · IllegalArgumentException
Values and weights arrays must not be null.
Error message
Values and weights arrays must not be null.
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOneTabulation.java:38
*/
public final class KnapsackZeroOneTabulation {
private KnapsackZeroOneTabulation() {
// Prevent instantiation
}
/**
* Solves the 0-1 Knapsack problem using the bottom-up tabulation technique.
* @param values the values of the items
* @param weights the weights of the items
* @param capacity the total capacity of the knapsack
* @param itemCount the number of items
* @return the maximum value that can be put in the knapsack
* @throws IllegalArgumentException if input arrays are null, of different lengths,or if capacity or itemCount is invalid
*/
public static int compute(final int[] values, final int[] weights, final int capacity, final int itemCount) {
if (values == null || weights == null) {
throw new IllegalArgumentException("Values and weights arrays must not be null.");
}
if (values.length != weights.length) {
throw new IllegalArgumentException("Values and weights arrays must be non-null and of same length.");
}
if (capacity < 0) {
throw new IllegalArgumentException("Capacity must not be negative.");
}
if (itemCount < 0 || itemCount > values.length) {
throw new IllegalArgumentException("Item count must be between 0 and the length of the values array.");
}
final int[][] dp = new int[itemCount + 1][capacity + 1];
for (int i = 1; i <= itemCount; i++) {
final int currentValue = values[i - 1];
final int currentWeight = weights[i - 1];
for (int w = 1; w <= capacity; w++) {View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before int r = KnapsackZeroOneTabulation.compute(values, null, cap, count); // throws // after int[] w = Objects.requireNonNullElseGet(weights, () -> new int[0]); int r = KnapsackZeroOneTabulation.compute(values, w, cap, Math.min(count, w.length));
Defensive patterns
Strategy: validation
Validate before calling
if (values == null || weights == null) {
throw new IllegalArgumentException("Arrays must not be null");
}
int r = KnapsackZeroOneTabulation.compute(values, weights, capacity, itemCount); Type guard
static boolean areNonNullArrays(int[] values, int[] weights) {
return values != null && weights != null;
} Prevention
- Default array fields to new int[0] instead of null.
- Use Objects.requireNonNull at the call boundary.
When it happens
Trigger: Calling compute(values, weights, capacity, itemCount) where values or weights is null — from uninitialized fields, failed JSON deserialization, or optional parameters left null.
Common situations: 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.
Related errors
- Weights and values must be non-null and of the same length.
- Input arrays cannot be null.
- Cannot enqueue null data
- Cannot enqueue null item.
- Input strings must not be null.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/8ff46d370d4d4801.
Report an issue: GitHub.