{"record":{"id":"b94557d08d718d14","repo":"TheAlgorithms/Java","slug":"x-and-y-must-be-non-null-non-empty-and-of-the-sa","errorCode":null,"errorMessage":"X and Y must be non-null, non-empty, and of the same length.","messagePattern":"X and Y must be non-null, non-empty, and of the same length\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/machinelearning/LinearRegression.java","lineNumber":37,"sourceCode":"     * @param epochs the number of iterations to train the model\n     */\n    public LinearRegression(double learningRate, int epochs) {\n        this.learningRate = learningRate;\n        this.epochs = epochs;\n        this.m = 0.0;\n        this.b = 0.0;\n    }\n\n    /**\n     * Trains the model on the provided dataset using batch gradient descent.\n     *\n     * @param x the input feature values\n     * @param y the corresponding target values\n     * @throws IllegalArgumentException if the arrays are null, empty, or of differing lengths\n     */\n    public void fit(double[] x, double[] y) {\n        if (x == null || y == null || x.length != y.length || x.length == 0) {\n            throw new IllegalArgumentException(\"X and Y must be non-null, non-empty, and of the same length.\");\n        }\n\n        int n = x.length;\n\n        for (int epoch = 0; epoch < epochs; epoch++) {\n            double mGradient = 0;\n            double bGradient = 0;\n\n            // Calculate gradients across the entire dataset\n            for (int i = 0; i < n; i++) {\n                double prediction = (m * x[i]) + b;\n                double error = prediction - y[i];\n\n                // Partial derivatives of the Mean Squared Error cost function\n                mGradient += error * x[i];\n                bGradient += error;\n            }\n","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/machinelearning/LinearRegression.java#L19-L55","documentation":"LinearRegression.fit performs batch gradient descent and requires the x and y arrays to be non-null, non-empty, and equal in length — there must be at least one training sample and every x must map to a y. A null, empty, or mismatched pair makes gradient computation impossible (division by zero on n, or ArrayIndexOutOfBounds).","triggerScenarios":"Calling fit(null, y), fit(x, null), fit(new double[0], new double[0]), or fit(x, y) where x.length != y.length.","commonSituations":"Empty dataset returned by a filtered query, a CSV parse that produced x and y columns of different lengths (e.g. a malformed row), or null passed when a data source returned no rows.","solutions":["Check x != null && y != null && x.length == y.length && x.length > 0 before calling fit.","If the dataset is empty, skip training (return early) rather than calling fit.","When loading paired data, assert equal lengths during load and drop/log mismatched rows."],"exampleFix":"// before\nmodel.fit(x, y); // x.length=0 from empty query\n\n// after\nif (x == null || y == null || x.length != y.length || x.length == 0) {\n    throw new IllegalArgumentException(\"invalid training data\");\n}\nmodel.fit(x, y);","handlingStrategy":"validation","validationCode":"if (x == null || y == null || x.length == 0 || x.length != y.length) {\n    throw new IllegalArgumentException(\"x and y must be non-null, non-empty, equal-length\");\n}\nmodel.fit(x, y);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate paired datasets at load time, rejecting rows where x or y is missing.","Skip training when the dataset is empty rather than calling fit.","Keep x and y coupled (e.g. a List<Point>) so lengths cannot drift apart."],"tags":["machine-learning","validation","null-check","empty-input","linear-regression","argument-mismatch"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}