TheAlgorithms/Java · error · IllegalArgumentException
Input arrays cannot be empty.
Error message
Input arrays cannot be empty.
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/maths/Neville.java:38
}
/**
* Evaluates the polynomial that passes through the given points at a
* specific x-coordinate.
*
* @param x The x-coordinates of the points. Must be the same length as y.
* @param y The y-coordinates of the points. Must be the same length as x.
* @param target The x-coordinate at which to evaluate the polynomial.
* @return The interpolated y-value at the target x-coordinate.
* @throws IllegalArgumentException if the lengths of x and y arrays are
* different, if the arrays are empty, or if x-coordinates are not unique.
*/
public static double interpolate(double[] x, double[] y, double target) {
if (x.length != y.length) {
throw new IllegalArgumentException("x and y arrays must have the same length.");
}
if (x.length == 0) {
throw new IllegalArgumentException("Input arrays cannot be empty.");
}
// Check for duplicate x-coordinates to prevent division by zero
Set<Double> seenX = new HashSet<>();
for (double val : x) {
if (!seenX.add(val)) {
throw new IllegalArgumentException("Input x-coordinates must be unique.");
}
}
int n = x.length;
double[] p = new double[n];
System.arraycopy(y, 0, p, 0, n); // Initialize p with y values
for (int k = 1; k < n; k++) {
for (int i = 0; i < n - k; i++) {
p[i] = ((target - x[i + k]) * p[i] + (x[i] - target) * p[i + 1]) / (x[i] - x[i + k]);
}View on GitHub (pinned to fdfb9a395b)
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
Example fix
// before
double result = Neville.interpolate(xPoints, yPoints, target);
// after
if (xPoints.length == 0) {
throw new IllegalStateException("No data points for interpolation");
}
double result = Neville.interpolate(xPoints, yPoints, target); Defensive patterns
Strategy: validation
Validate before calling
if (x == null || x.length == 0) {
throw new IllegalStateException("No data points available for interpolation");
}
double result = Neville.interpolate(x, y, target); Type guard
static boolean hasDataPoints(double[] x) {
return x != null && x.length > 0;
} Try / catch
try {
double result = Neville.interpolate(x, y, target);
} catch (IllegalArgumentException e) {
// empty input — no points to interpolate through
return Optional.empty();
} Prevention
- 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
When it happens
Trigger: Calling interpolate(new double[]{}, new double[]{}, 1.5) or interpolate(emptyXArray, emptyYArray, target). Hit when data loading produces no points.
Common situations: 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.
Related errors
- x and y arrays must have the same length.
- Number must be non-negative. Given:
- Number must be positive
- Input must be non-negative. Received:
- Base must be greater than 1.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/cc4215c121c61178.
Report an issue: GitHub.