TheAlgorithms/C-Sharp · error · ArgumentNullException

input

Error message

input

What it means

Relu.Compute applies max(0, x) elementwise to a vector of doubles. It throws ArgumentNullException when the input array is null, using the parameter name 'input' as the message. Callers must supply an allocated array; an empty array is separately rejected with 'Array is empty.'.

Solutions

  1. Ensure the array is allocated before calling; use Array.Empty<double>() if you truly have no data (note empty arrays still throw the sibling error).
  2. Add a null check at the boundary where the array is produced.
  3. Coalesce null to a default vector if that fits your semantics.

Example fix

// before
var output = Relu.Compute(data); // data may be null
// after
var output = Relu.Compute(data ?? throw new ArgumentNullException(nameof(data)));
Defensive patterns

Strategy: validation

Validate before calling

if (input is null) throw new ArgumentNullException(nameof(input));
var output = Relu.Compute(input);

Type guard

static bool IsValidReluInput(double[]? a) => a is { Length: > 0 };

Try / catch

try { var out = Relu.Compute(input); }
catch (ArgumentNullException) { /* supply a default vector or propagate a domain error */ }

Prevention

When it happens

Trigger: Calling Relu.Compute(null), typically when an upstream method returned a null array or a deserialization produced null.

Common situations: A lookup/parse function returning null instead of an array, or an optional input field not being defaulted before inference-style numeric processing.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/f5219d6bdc825a83. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Numeric/Relu.cs:29

    ///     Compute the Rectified Linear Unit (ReLU) for a single value.
    /// </summary>
    /// <param name="input">The input real number.</param>
    /// <returns>The output real number (>= 0).</returns>
    public static double Compute(double input)
    {
        return Math.Max(0.0, input);
    }

    /// <summary>
    ///     Compute the Rectified Linear Unit (ReLU) element-wise for a vector.
    /// </summary>
    /// <param name="input">The input vector of real numbers.</param>
    /// <returns>The output vector where each element is max(0, input[i]).</returns>
    public static double[] Compute(double[] input)
    {
        if (input is null)
        {
            throw new ArgumentNullException(nameof(input));
        }

        if (input.Length == 0)
        {
            throw new ArgumentException("Array is empty.");
        }

        var output = new double[input.Length];

        for (var i = 0; i < input.Length; i++)
        {
            output[i] = Math.Max(0.0, input[i]);
        }

        return output;
    }
}

View on GitHub (pinned to 96e2905cab)