{"record":{"id":"94241ca68c4d15c3","repo":"TheAlgorithms/Java","slug":"points-must-have-the-same-dimension","errorCode":null,"errorMessage":"Points must have the same dimension","messagePattern":"Points must have the same dimension","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/trees/KDTree.java","lineNumber":43,"sourceCode":"     * @param k Number of dimensions\n     */\n    KDTree(int k) {\n        this.k = k;\n    }\n\n    /**\n     * Builds the KDTree from the specified points\n     *\n     * @param points Array of initial points\n     */\n    KDTree(Point[] points) {\n        if (points.length == 0) {\n            throw new IllegalArgumentException(\"Points array cannot be empty\");\n        }\n        this.k = points[0].getDimension();\n        for (Point point : points) {\n            if (point.getDimension() != k) {\n                throw new IllegalArgumentException(\"Points must have the same dimension\");\n            }\n        }\n        this.root = build(points, 0);\n    }\n\n    /**\n     * Builds the KDTree from the specified coordinates of the points\n     *\n     * @param pointsCoordinates Array of initial points coordinates\n     *\n     */\n    KDTree(int[][] pointsCoordinates) {\n        if (pointsCoordinates.length == 0) {\n            throw new IllegalArgumentException(\"Points array cannot be empty\");\n        }\n        this.k = pointsCoordinates[0].length;\n        Point[] points = Arrays.stream(pointsCoordinates).map(Point::new).toArray(Point[] ::new);\n        for (Point point : points) {","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/trees/KDTree.java#L25-L61","documentation":"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.","triggerScenarios":"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.","commonSituations":"Heterogeneous datasets joined without normalization. Serialization that omits trailing coordinates for some records. Feature vectors of varying length from an upstream pipeline.","solutions":["Ensure all points have identical dimensionality before constructing; pad or reject mismatches.","Validate getDimension() across the set at the input boundary.","Partition points by dimension and build separate KDTree instances per group.","Fix the upstream producer to emit consistent dimensions."],"exampleFix":"// before\nKDTree tree = new KDTree(points);\n// after\nint k = points[0].getDimension();\nfor (Point p : points) {\n    if (p.getDimension() != k) {\n        throw new IllegalArgumentException(\"inconsistent point dimension\");\n    }\n}\nKDTree tree = new KDTree(points);","handlingStrategy":"validation","validationCode":"int k = points[0].getDimension();\nfor (Point p : points) {\n    if (p.getDimension() != k) {\n        throw new IllegalArgumentException(\"inconsistent point dimension\");\n    }\n}\nKDTree tree = new KDTree(points);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify all points share the same dimension before constructing.","Partition heterogeneous points by dimension.","Fix producers to emit consistent feature counts."],"tags":["tree","invalid-argument","java","datastructures","kdtree","dimension"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}