{"record":{"id":"e4dc6326241a7851","repo":"TheAlgorithms/C-Sharp","slug":"input-data-cannot-be-empty","errorCode":null,"errorMessage":"Input data cannot be empty.","messagePattern":"Input data cannot be empty\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/MachineLearning/LinearRegression.cs","lineNumber":37,"sourceCode":"\n    public bool IsFitted { get; private set; }\n\n    /// <summary>\n    /// Fits the linear regression model to the provided data.\n    /// </summary>\n    /// <param name=\"x\">List of independent variable values.</param>\n    /// <param name=\"y\">List of dependent variable values.</param>\n    /// <exception cref=\"ArgumentException\">Thrown if input lists are null, empty, or of different lengths.</exception>\n    public void Fit(IList<double> x, IList<double> y)\n    {\n        if (x == null || y == null)\n        {\n            throw new ArgumentException(\"Input data cannot be null.\");\n        }\n\n        if (x.Count == 0 || y.Count == 0)\n        {\n            throw new ArgumentException(\"Input data cannot be empty.\");\n        }\n\n        if (x.Count != y.Count)\n        {\n            throw new ArgumentException(\"Input lists must have the same length.\");\n        }\n\n        // Calculate means\n        double xMean = x.Average();\n        double yMean = y.Average();\n\n        // Calculate slope (b) and intercept (a)\n        double numerator = 0.0;\n        double denominator = 0.0;\n        for (int i = 0; i < x.Count; i++)\n        {\n            numerator += (x[i] - xMean) * (y[i] - yMean);\n            denominator += (x[i] - xMean) * (x[i] - xMean);","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/MachineLearning/LinearRegression.cs#L19-L55","documentation":"Fit validates its inputs before fitting: null x or y lists, empty lists, or mismatched lengths make regression impossible (zero/n undefined variance), so a generic ArgumentException is thrown; this record fires for the null-list guard.","triggerScenarios":"Calling Fit with x.Count == 0 or y.Count == 0 — e.g. after filtering removed all rows or an empty file was parsed into empty lists.","commonSituations":"Empty CSV or query returning no rows; over-aggressive filtering (e.g. removing NaNs) leaving no data; passing new List<double>() placeholders during development.","solutions":["Ensure at least one (x, y) observation pair exists before calling Fit.","After loading/filtering data, check Count > 0 and surface a clear message instead of fitting.","Fix the loading/filtering logic that removed all observations."],"exampleFix":"// before\nvar xs = rows.Where(r => r.X > threshold).Select(r => r.X).ToList();\nregression.Fit(xs, ys); // may be empty\n// after\nif (xs.Count == 0) throw new InvalidOperationException(\"No data after filtering.\");\nregression.Fit(xs, ys);","handlingStrategy":"validation","validationCode":"if (xs.Count == 0 || ys.Count == 0)\n    throw new InvalidOperationException(\"Cannot fit: dataset has no observations.\");\nregression.Fit(xs, ys);","typeGuard":null,"tryCatchPattern":"try\n{\n    regression.Fit(xs, ys);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"empty\"))\n{\n    // no observations after loading/filtering\n    logger.LogError(ex, \"Empty dataset supplied to Fit\");\n}","preventionTips":["Check row count after loading and filtering, before fitting.","Log how many rows survived filtering so silent full-filtering is visible.","Fail the pipeline with a clear 'no data' message instead of letting Fit throw."],"tags":["csharp","empty-input","machine-learning","linear-regression","fit"],"backgroundTag":"empty-required-field","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}