TheAlgorithms/Java · error · IllegalArgumentException
Input arrays cannot be null.
Error message
Input arrays cannot be null.
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOne.java:32
private KnapsackZeroOne() {
// Prevent instantiation
}
/**
* Solves the 0/1 Knapsack problem using recursion.
*
* @param values the array containing values of the items
* @param weights the array containing weights of the items
* @param capacity the total capacity of the knapsack
* @param n the number of items
* @return the maximum total value achievable within the given weight limit
* @throws IllegalArgumentException if input arrays are null, empty, or
* lengths mismatch
*/
public static int compute(final int[] values, final int[] weights, final int capacity, final int n) {
if (values == null || weights == null) {
throw new IllegalArgumentException("Input arrays cannot be null.");
}
if (values.length != weights.length) {
throw new IllegalArgumentException("Value and weight arrays must be of the same length.");
}
if (capacity < 0 || n < 0) {
throw new IllegalArgumentException("Invalid input: arrays must be non-empty and capacity/n "
+ "non-negative.");
}
if (n == 0 || capacity == 0 || values.length == 0) {
return 0;
}
if (weights[n - 1] <= capacity) {
final int include = values[n - 1] + compute(values, weights, capacity - weights[n - 1], n - 1);
final int exclude = compute(values, weights, capacity, n - 1);
return Math.max(include, exclude);
} else {
return compute(values, weights, capacity, n - 1);View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before int r = KnapsackZeroOne.compute(null, weights, cap, n); // throws // after int[] vals = Objects.requireNonNullElseGet(values, () -> new int[0]); int r = KnapsackZeroOne.compute(vals, weights, cap, n);
Defensive patterns
Strategy: validation
Validate before calling
if (values == null || weights == null) {
throw new IllegalArgumentException("Arrays must not be null");
}
int r = KnapsackZeroOne.compute(values, weights, capacity, n); 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, n) where values or weights is null — typically from an uninitialized field, a failed deserialization, or a default null placeholder.
Common situations: 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.
Related errors
- Weights and values must be non-null and of the same length.
- Values and weights arrays must not 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/1d5ff72e0018b884.
Report an issue: GitHub.