TheAlgorithms/Java · error · IllegalArgumentException

Price array cannot be null or empty.

Error message

Price array cannot be null or empty.

What it means

Thrown by RodCutting.cutRod(int[] price, int n) when price is null or empty. The algorithm indexes price[j-1] for j up to n; an empty/null array breaks immediately. Message: 'Price array cannot be null or empty.'

Source

Thrown at src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java:23

 * Returns the best obtainable price for a rod of length n and price[] as prices of different pieces.
 */
public final class RodCutting {
    private RodCutting() {
    }

    /**
     * This method calculates the maximum obtainable value for cutting a rod of length n
     * into different pieces, given the prices for each possible piece length.
     *
     * @param price An array representing the prices of different pieces, where price[i-1]
     *              represents the price of a piece of length i.
     * @param n     The length of the rod to be cut.
     * @throws IllegalArgumentException if the price array is null or empty, or if n is less than 0.
     * @return The maximum obtainable value.
     */
    public static int cutRod(int[] price, int n) {
        if (price == null || price.length == 0) {
            throw new IllegalArgumentException("Price array cannot be null or empty.");
        }
        if (n < 0) {
            throw new IllegalArgumentException("Rod length cannot be negative.");
        }

        // Create an array to store the maximum obtainable values for each rod length.
        int[] val = new int[n + 1];
        val[0] = 0;

        // Calculate the maximum value for each rod length from 1 to n.
        for (int i = 1; i <= n; i++) {
            int maxVal = Integer.MIN_VALUE;
            // Try all possible ways to cut the rod and find the maximum value.
            for (int j = 1; j <= i; j++) {
                maxVal = Math.max(maxVal, price[j - 1] + val[i - j]);
            }
            // Store the maximum value for the current rod length.
            val[i] = maxVal;

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure the price array has at least one entry (and ideally n entries) before calling cutRod.
  2. Validate price != null && price.length > 0 at the boundary.
  3. Load a sensible default price table when the source is empty.

Example fix

// before
int best = RodCutting.cutRod(prices, n);

// after
if (prices == null || prices.length == 0) {
    throw new IllegalArgumentException("prices required");
}
int best = RodCutting.cutRod(prices, n);
Defensive patterns

Strategy: validation

Validate before calling

if (price == null || price.length == 0) {
    throw new IllegalArgumentException("price array required");
}
RodCutting.cutRod(price, n);

Type guard

price != null && price.length > 0

Prevention

When it happens

Trigger: Passing null or a zero-length price array; loading prices from an empty file/config; defaulting to an uninitialized array field.

Common situations: Configuration file missing the prices section; rod length n exceeds available prices but the array itself is empty; tests with an empty fixture.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/b59913fa28de6bc0. Report an issue: GitHub.