{"record":{"id":"cc4215c121c61178","repo":"TheAlgorithms/Java","slug":"input-arrays-cannot-be-empty","errorCode":null,"errorMessage":"Input arrays cannot be empty.","messagePattern":"Input arrays cannot be empty\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/Neville.java","lineNumber":38,"sourceCode":"    }\n\n    /**\n     * Evaluates the polynomial that passes through the given points at a\n     * specific x-coordinate.\n     *\n     * @param x The x-coordinates of the points. Must be the same length as y.\n     * @param y The y-coordinates of the points. Must be the same length as x.\n     * @param target The x-coordinate at which to evaluate the polynomial.\n     * @return The interpolated y-value at the target x-coordinate.\n     * @throws IllegalArgumentException if the lengths of x and y arrays are\n     * different, if the arrays are empty, or if x-coordinates are not unique.\n     */\n    public static double interpolate(double[] x, double[] y, double target) {\n        if (x.length != y.length) {\n            throw new IllegalArgumentException(\"x and y arrays must have the same length.\");\n        }\n        if (x.length == 0) {\n            throw new IllegalArgumentException(\"Input arrays cannot be empty.\");\n        }\n\n        // Check for duplicate x-coordinates to prevent division by zero\n        Set<Double> seenX = new HashSet<>();\n        for (double val : x) {\n            if (!seenX.add(val)) {\n                throw new IllegalArgumentException(\"Input x-coordinates must be unique.\");\n            }\n        }\n\n        int n = x.length;\n        double[] p = new double[n];\n        System.arraycopy(y, 0, p, 0, n); // Initialize p with y values\n\n        for (int k = 1; k < n; k++) {\n            for (int i = 0; i < n - k; i++) {\n                p[i] = ((target - x[i + k]) * p[i] + (x[i] - target) * p[i + 1]) / (x[i] - x[i + k]);\n            }","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/Neville.java#L20-L56","documentation":"Thrown by Neville.interpolate(double[] x, double[] y, double target) when both arrays are empty (x.length == 0). This check runs after the length-equality check, so it fires only when x and y are both empty (and thus equal in length). Interpolating through zero points is undefined.","triggerScenarios":"Calling interpolate(new double[]{}, new double[]{}, 1.5) or interpolate(emptyXArray, emptyYArray, target). Hit when data loading produces no points.","commonSituations":"Empty data set from a filtered query or a sensor that returned no readings. Default-initialized arrays that were never populated. Edge case where the x and y arrays are both empty but have matching lengths.","solutions":["Check that x.length > 0 (and y.length > 0) before calling interpolate","Handle empty data sets at the application layer with a default value or error","Guard the data-loading code to reject empty point sets before they reach interpolation"],"exampleFix":"// before\ndouble result = Neville.interpolate(xPoints, yPoints, target);\n\n// after\nif (xPoints.length == 0) {\n    throw new IllegalStateException(\"No data points for interpolation\");\n}\ndouble result = Neville.interpolate(xPoints, yPoints, target);","handlingStrategy":"validation","validationCode":"if (x == null || x.length == 0) {\n    throw new IllegalStateException(\"No data points available for interpolation\");\n}\ndouble result = Neville.interpolate(x, y, target);","typeGuard":"static boolean hasDataPoints(double[] x) {\n    return x != null && x.length > 0;\n}","tryCatchPattern":"try {\n    double result = Neville.interpolate(x, y, target);\n} catch (IllegalArgumentException e) {\n    // empty input — no points to interpolate through\n    return Optional.empty();\n}","preventionTips":["Check that the data set has at least one point before calling interpolate","Handle empty query results or sensor readings before they reach the interpolation layer","This guard fires only when both arrays are empty and equal in length — mismatched lengths hit a different error first"],"tags":["math","validation","illegal-argument","interpolation","empty-array"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}