TheAlgorithms/Java · error · IllegalArgumentException

Points must have the same dimension

Error message

Points must have the same dimension

What it means

Thrown by the KDTree(Point[]) constructor when not all points share the same dimensionality. The constructor infers k from points[0] and then requires every point's getDimension() to equal k, since the tree splits on axis depth%k. Mismatched dimensions would corrupt the build. The IllegalArgumentException enforces dimensional homogeneity.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/trees/KDTree.java:43

     * @param k Number of dimensions
     */
    KDTree(int k) {
        this.k = k;
    }

    /**
     * Builds the KDTree from the specified points
     *
     * @param points Array of initial points
     */
    KDTree(Point[] points) {
        if (points.length == 0) {
            throw new IllegalArgumentException("Points array cannot be empty");
        }
        this.k = points[0].getDimension();
        for (Point point : points) {
            if (point.getDimension() != k) {
                throw new IllegalArgumentException("Points must have the same dimension");
            }
        }
        this.root = build(points, 0);
    }

    /**
     * Builds the KDTree from the specified coordinates of the points
     *
     * @param pointsCoordinates Array of initial points coordinates
     *
     */
    KDTree(int[][] pointsCoordinates) {
        if (pointsCoordinates.length == 0) {
            throw new IllegalArgumentException("Points array cannot be empty");
        }
        this.k = pointsCoordinates[0].length;
        Point[] points = Arrays.stream(pointsCoordinates).map(Point::new).toArray(Point[] ::new);
        for (Point point : points) {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure all points have identical dimensionality before constructing; pad or reject mismatches.
  2. Validate getDimension() across the set at the input boundary.
  3. Partition points by dimension and build separate KDTree instances per group.
  4. Fix the upstream producer to emit consistent dimensions.

Example fix

// before
KDTree tree = new KDTree(points);
// after
int k = points[0].getDimension();
for (Point p : points) {
    if (p.getDimension() != k) {
        throw new IllegalArgumentException("inconsistent point dimension");
    }
}
KDTree tree = new KDTree(points);
Defensive patterns

Strategy: validation

Validate before calling

int k = points[0].getDimension();
for (Point p : points) {
    if (p.getDimension() != k) {
        throw new IllegalArgumentException("inconsistent point dimension");
    }
}
KDTree tree = new KDTree(points);

Prevention

When it happens

Trigger: Mixing 2D and 3D points in one array. Passing points where some have default/zero coordinates due to a parsing bug. Concatenating point sets from sources with different dimensionality.

Common situations: Heterogeneous datasets joined without normalization. Serialization that omits trailing coordinates for some records. Feature vectors of varying length from an upstream pipeline.

Related errors


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