{"record":{"id":"b59913fa28de6bc0","repo":"TheAlgorithms/Java","slug":"price-array-cannot-be-null-or-empty","errorCode":null,"errorMessage":"Price array cannot be null or empty.","messagePattern":"Price array cannot be null or empty\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java","lineNumber":23,"sourceCode":" * Returns the best obtainable price for a rod of length n and price[] as prices of different pieces.\n */\npublic final class RodCutting {\n    private RodCutting() {\n    }\n\n    /**\n     * This method calculates the maximum obtainable value for cutting a rod of length n\n     * into different pieces, given the prices for each possible piece length.\n     *\n     * @param price An array representing the prices of different pieces, where price[i-1]\n     *              represents the price of a piece of length i.\n     * @param n     The length of the rod to be cut.\n     * @throws IllegalArgumentException if the price array is null or empty, or if n is less than 0.\n     * @return The maximum obtainable value.\n     */\n    public static int cutRod(int[] price, int n) {\n        if (price == null || price.length == 0) {\n            throw new IllegalArgumentException(\"Price array cannot be null or empty.\");\n        }\n        if (n < 0) {\n            throw new IllegalArgumentException(\"Rod length cannot be negative.\");\n        }\n\n        // Create an array to store the maximum obtainable values for each rod length.\n        int[] val = new int[n + 1];\n        val[0] = 0;\n\n        // Calculate the maximum value for each rod length from 1 to n.\n        for (int i = 1; i <= n; i++) {\n            int maxVal = Integer.MIN_VALUE;\n            // Try all possible ways to cut the rod and find the maximum value.\n            for (int j = 1; j <= i; j++) {\n                maxVal = Math.max(maxVal, price[j - 1] + val[i - j]);\n            }\n            // Store the maximum value for the current rod length.\n            val[i] = maxVal;","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java#L5-L41","documentation":"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.'","triggerScenarios":"Passing null or a zero-length price array; loading prices from an empty file/config; defaulting to an uninitialized array field.","commonSituations":"Configuration file missing the prices section; rod length n exceeds available prices but the array itself is empty; tests with an empty fixture.","solutions":["Ensure the price array has at least one entry (and ideally n entries) before calling cutRod.","Validate price != null && price.length > 0 at the boundary.","Load a sensible default price table when the source is empty."],"exampleFix":"// before\nint best = RodCutting.cutRod(prices, n);\n\n// after\nif (prices == null || prices.length == 0) {\n    throw new IllegalArgumentException(\"prices required\");\n}\nint best = RodCutting.cutRod(prices, n);","handlingStrategy":"validation","validationCode":"if (price == null || price.length == 0) {\n    throw new IllegalArgumentException(\"price array required\");\n}\nRodCutting.cutRod(price, n);","typeGuard":"price != null && price.length > 0","tryCatchPattern":null,"preventionTips":["Load a default price table when the source is empty.","Ensure price has at least n entries for the requested rod length.","Validate the array at the configuration boundary."],"tags":["null-check","array","dynamic-programming","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}