TheAlgorithms/C-Sharp · error · ArgumentNullException
Input cannot be null
Error message
Input cannot be null
What it means
JaccardSimilarity.ValidateInput, called from Calculate, rejects null left or right strings with ArgumentNullException carrying message "Input cannot be null" and the offending parameter name. Empty strings are acceptable (their token sets are empty), only null throws. This protects set-based similarity computation from null dereference.
Solutions
- Coalesce nulls to empty before calling: Calculate(left ?? "", right ?? "").
- Null-check both strings at the call site and skip/short-circuit the similarity computation.
- Fix the upstream data source to return empty strings rather than null.
- Catch ArgumentNullException if null inputs are a valid runtime condition.
Example fix
// before var score = jaccard.Calculate(docA.Text, docB.Text); // either may be null // after var score = jaccard.Calculate(docA.Text ?? string.Empty, docB.Text ?? string.Empty);
Defensive patterns
Strategy: validation
Validate before calling
if (left == null || right == null)
{
// skip computation or coalesce: left ?? "" ; right ?? ""
} Type guard
static bool BothNonNull(string? a, string? b) => a != null && b != null;
Try / catch
try
{
score = jaccard.Calculate(left, right);
}
catch (ArgumentNullException ex)
{
score = 0.0; // define similarity of null input as zero
} Prevention
- Coalesce nulls to empty strings at data-loading boundaries.
- Treat missing text fields as empty sets, not nulls, in similarity pipelines.
- Enable nullable reference types to surface null flow at compile time.
- Unit-test Calculate(null, x), Calculate(x, null), and Calculate(null, null).
When it happens
Trigger: Calling Calculate(null, "abc"), Calculate("abc", null), or Calculate(null, null) — the paramName reported is whichever argument is null (left wins if both are null).
Common situations: Nullable document/text fields from a database; deserialized objects with missing string properties; pipeline steps that emit null instead of empty content.
Related errors
- ArgumentNullException: vertices
- ArgumentNullException: getNeighbors
- ArgumentNullException: graph
- ArgumentNullException: features
- Input data cannot be null.
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/ec3c29348b704bfa.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Strings/Similarity/JaccardSimilarity.cs:92
// Calculate the intersection size of the two strings.
var intersectionSize = leftSet.Count + rightSet.Count - unionSet.Count;
// Return the Jaccard similarity coefficient as the ratio of intersection to union.
return 1.0d * intersectionSize / unionSet.Count;
}
/// <summary>
/// Validates the input strings and throws an exception if either is null.
/// </summary>
/// <param name="left">The first string to validate.</param>
/// <param name="right">The second string to validate.</param>
private void ValidateInput(string left, string right)
{
if (left == null || right == null)
{
var paramName = left == null ? nameof(left) : nameof(right);
throw new ArgumentNullException(paramName, "Input cannot be null");
}
}
}
View on GitHub (pinned to 96e2905cab)