TheAlgorithms/C-Sharp · error · ArgumentNullException
input
Error message
input
What it means
Tanh.Compute applies the hyperbolic tangent element-wise and requires a non-null input array. A null reference cannot be iterated, so the method throws ArgumentNullException naming the 'input' parameter instead of failing with a NullReferenceException.
Solutions
- Initialize the array before calling (e.g. Array.Empty<double>() instead of null)
- Add a null check at the call site and skip/return a default result when null
- Fix the producer that returns null to return an empty array instead
Example fix
// before double[] activations = null; var out = Tanh.Compute(activations); // throws // after var out = Tanh.Compute(activations ?? Array.Empty<double>());
Defensive patterns
Strategy: type-guard
Validate before calling
if (input is null) throw new ArgumentNullException(nameof(input));
Type guard
static bool IsNotNull<T>(T[]? a) => a is not null;
Try / catch
try { return Tanh.Compute(input); }
catch (ArgumentNullException ex) when (ex.ParamName == "input") { /* initialize or skip */ } Prevention
- Initialize array fields with Array.Empty<T>() at declaration
- Avoid returning null arrays from methods; prefer empty arrays
- Enable nullable reference types to catch nulls at compile time
When it happens
Trigger: Calling Tanh.Compute(null), typically when an array field/property was never initialized or a method returned null instead of an empty array.
Common situations: Uninitialized class fields for activations; a lookup that returned null in older API versions; optional configuration vectors left unset.
Related errors
- Error value is not on interval (0.0; 1.0).
- Array is empty.
- Input must be a non-negative integer.
- Array is empty.
- Invalid parameter settings for Ascon Hash
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/3447e62864194f91.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Numeric/Tanh.cs:33
/// </summary>
/// <param name="input">The input real number.</param>
/// <returns>The output real number in the range [-1, 1].</returns>
public static double Compute(double input)
{
// For a single double, we can directly use the optimized Math.Tanh method.
return Math.Tanh(input);
}
/// <summary>
/// Compute the Hyperbolic Tangent (Tanh) function element-wise for a vector.
/// </summary>
/// <param name="input">The input vector of real numbers.</param>
/// <returns>The output vector of real numbers, where each element is in the range [-1, 1].</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 outputVector = new double[input.Length];
for (var index = 0; index < input.Length; index++)
{
// Apply Tanh to each element using the optimized Math.Tanh method.
outputVector[index] = Math.Tanh(input[index]);
}
return outputVector;
}
}View on GitHub (pinned to 96e2905cab)