{"record":{"id":"c0e49ed12a2bd69f","repo":"TheAlgorithms/C-Sharp","slug":"input-data-cannot-be-null","errorCode":null,"errorMessage":"Input data cannot be null.","messagePattern":"Input data cannot be null\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/MachineLearning/LinearRegression.cs","lineNumber":32,"sourceCode":"{\n    // Intercept (a) and slope (b) of the fitted line\n    public double Intercept { get; private set; }\n\n    public double Slope { get; private set; }\n\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;","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/MachineLearning/LinearRegression.cs#L14-L50","documentation":"LinearRegression.Fit requires two non-null lists of observations, x (independent) and y (dependent); nulls cannot be measured for count/variance. The library throws ArgumentException('Input data cannot be null.') when either list is null.","triggerScenarios":"Calling Fit(null, y), Fit(x, null), or Fit(a, b) where either argument is null from a failed data load or uninitialized variable.","commonSituations":"CSV/JSON loading producing null lists on file-not-found or parse failure; optional dataset fields left null; refactoring where data assignment was removed.","solutions":["Pass non-null lists for both x and y.","Check the data-loading step: if the source file/parse failed, the lists may be null — handle that before calling Fit.","Initialize lists to empty collections rather than leaving them null, then rely on the empty check."],"exampleFix":"// before\nregression.Fit(xData, yData); // yData null when file load failed\n// after\nif (xData == null || yData == null)\n    throw new InvalidOperationException(\"Data not loaded.\");\nregression.Fit(xData, yData);","handlingStrategy":"validation","validationCode":"if (xs == null || ys == null)\n    throw new InvalidOperationException(\"Regression data not loaded.\");\nregression.Fit(xs, ys);","typeGuard":"static bool CanFit(IList<double>? x, IList<double>? y) => x != null && y != null;","tryCatchPattern":"try\n{\n    regression.Fit(xs, ys);\n}\ncatch (ArgumentException ex)\n{\n    // null / empty / length-mismatch inputs — check ex.Message to distinguish\n    logger.LogError(ex, \"Fit rejected input data\");\n}","preventionTips":["Initialize x and y lists at declaration so they are never null.","Check data-loading success before the training step (file found, rows parsed).","Prefer empty lists plus an explicit Count check over null sentinels."],"tags":["csharp","null-argument","machine-learning","linear-regression","fit"],"backgroundTag":"null-argument","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"}