TheAlgorithms/Java · error · IllegalArgumentException
Input array cannot be null or empty.
Error message
Input array cannot be null or empty.
What it means
Thrown by WineProblem.wpbu(int[] arr) when arr is null or empty. The bottom-up DP allocates strg = new int[n][n] where n = arr.length; an empty array yields a degenerate 0x0 table and null NPEs on access. Message: 'Input array cannot be null or empty.'
Source
Thrown at src/main/java/com/thealgorithms/dynamicprogramming/WineProblem.java:88
int start = wptd(arr, si + 1, ei, strg) + arr[si] * year;
int end = wptd(arr, si, ei - 1, strg) + arr[ei] * year;
int ans = Math.max(start, end);
strg[si][ei] = ans;
return ans;
}
/**
* Calculate maximum profit using bottom-up dynamic programming with tabulation.
*
* @param arr Array of wine prices.
* @throws IllegalArgumentException if the input array is null or empty.
* @return Maximum profit obtainable by selling the wines.
*/
public static int wpbu(int[] arr) {
if (arr == null || arr.length == 0) {
throw new IllegalArgumentException("Input array cannot be null or empty.");
}
int n = arr.length;
int[][] strg = new int[n][n];
for (int slide = 0; slide <= n - 1; slide++) {
for (int si = 0; si <= n - slide - 1; si++) {
int ei = si + slide;
int year = (n - (ei - si + 1)) + 1;
if (si == ei) {
strg[si][ei] = arr[si] * year;
} else {
int start = strg[si + 1][ei] + arr[si] * year;
int end = strg[si][ei - 1] + arr[ei] * year;
strg[si][ei] = Math.max(start, end);
}
}
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure the array has at least one element before calling wpbu.
- Validate arr != null && arr.length > 0 at the boundary.
- Default to a sensible non-empty input or report the empty source upstream.
Example fix
// before
int profit = WineProblem.wpbu(arr);
// after
if (arr == null || arr.length == 0) throw new IllegalArgumentException("prices required");
int profit = WineProblem.wpbu(arr); Defensive patterns
Strategy: validation
Validate before calling
if (arr == null || arr.length == 0) {
throw new IllegalArgumentException("wine prices required");
}
WineProblem.wpbu(arr); Type guard
arr != null && arr.length > 0
Prevention
- Default to a non-empty price array when the source is empty.
- Validate at the data-load boundary.
- Add a test for the empty-array case.
When it happens
Trigger: Passing null or an empty price array; loading wine prices from an empty source; uninitialized array field.
Common situations: Reading prices from a config/file that was empty; tests with an empty fixture; refactoring that left the array unset.
Related errors
- Keys and frequencies cannot be null
- Price array cannot be null or empty.
- Input string must not be null
- Input array should not contain negative number(s).
- Keys and frequencies must have the same length
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/b7d9abb7a7493c76.
Report an issue: GitHub.