TheAlgorithms/C-Sharp · error · ArgumentException
Input data cannot be null.
Error message
Input data cannot be null.
What it means
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.
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.
Example fix
// before
regression.Fit(xData, yData); // yData null when file load failed
// after
if (xData == null || yData == null)
throw new InvalidOperationException("Data not loaded.");
regression.Fit(xData, yData); Defensive patterns
Strategy: validation
Validate before calling
if (xs == null || ys == null)
throw new InvalidOperationException("Regression data not loaded.");
regression.Fit(xs, ys); Type guard
static bool CanFit(IList<double>? x, IList<double>? y) => x != null && y != null;
Try / catch
try
{
regression.Fit(xs, ys);
}
catch (ArgumentException ex)
{
// null / empty / length-mismatch inputs — check ex.Message to distinguish
logger.LogError(ex, "Fit rejected input data");
} Prevention
- 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.
When it happens
Trigger: Calling Fit(null, y), Fit(x, null), or Fit(a, b) where either argument is null from a failed data load or uninitialized variable.
Common situations: CSV/JSON loading producing null lists on file-not-found or parse failure; optional dataset fields left null; refactoring where data assignment was removed.
Related errors
- Input data cannot be empty.
- ArgumentNullException: features
- Input lists must have the same length.
- Variance of X must not be zero.
- Model must be fitted before prediction.
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/c0e49ed12a2bd69f.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/MachineLearning/LinearRegression.cs:32
{
// Intercept (a) and slope (b) of the fitted line
public double Intercept { get; private set; }
public double Slope { get; private set; }
public bool IsFitted { get; private set; }
/// <summary>
/// Fits the linear regression model to the provided data.
/// </summary>
/// <param name="x">List of independent variable values.</param>
/// <param name="y">List of dependent variable values.</param>
/// <exception cref="ArgumentException">Thrown if input lists are null, empty, or of different lengths.</exception>
public void Fit(IList<double> x, IList<double> y)
{
if (x == null || y == null)
{
throw new ArgumentException("Input data cannot be null.");
}
if (x.Count == 0 || y.Count == 0)
{
throw new ArgumentException("Input data cannot be empty.");
}
if (x.Count != y.Count)
{
throw new ArgumentException("Input lists must have the same length.");
}
// Calculate means
double xMean = x.Average();
double yMean = y.Average();
// Calculate slope (b) and intercept (a)
double numerator = 0.0;View on GitHub (pinned to 96e2905cab)