{"record":{"id":"004220dd2699cf86","repo":"TheAlgorithms/Java","slug":"points-array-cannot-be-empty","errorCode":null,"errorMessage":"Points array cannot be empty","messagePattern":"Points array cannot be empty","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/trees/KDTree.java","lineNumber":38,"sourceCode":"    private final int k; // Dimensions of the points\n\n    /**\n     * Constructor for empty KDTree\n     *\n     * @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) {","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/trees/KDTree.java#L20-L56","documentation":"Thrown by the KDTree(Point[]) constructor when the points array has length 0. The constructor reads points[0].getDimension() to infer k, so an empty array would ArrayIndexOutOfBoundsException. The IllegalArgumentException rejects an empty input before any dimension inference.","triggerScenarios":"Constructing a KDTree from an empty collection converted to an array. Passing a points array built by filtering that yielded no matches. Passing a test fixture with no points.","commonSituations":"Dynamic point sets that can be empty after filtering. Deserialization of an empty dataset. Pipeline stages where an upstream filter removed all points.","solutions":["Skip KDTree construction when the point set is empty; handle the empty case separately.","Ensure at least one point is present before calling the constructor.","Use Optional or an explicit empty-state branch in the caller.","Validate collection size at the input boundary."],"exampleFix":"// before\nKDTree tree = new KDTree(points);\n// after\nif (points.length == 0) {\n    return Optional.empty();\n}\nKDTree tree = new KDTree(points);","handlingStrategy":"validation","validationCode":"if (points.length > 0) {\n    KDTree tree = new KDTree(points);\n} else {\n    // handle empty point set\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Handle the empty-point-set case before constructing.","Validate collection size at the input boundary.","Use Optional to represent a possibly-absent tree."],"tags":["tree","invalid-argument","java","datastructures","kdtree","empty-state"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}