{"record":{"id":"ec3c29348b704bfa","repo":"TheAlgorithms/C-Sharp","slug":"input-cannot-be-null-jaccardsimilarity","errorCode":null,"errorMessage":"Input cannot be null","messagePattern":"Input cannot be null","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Algorithms/Strings/Similarity/JaccardSimilarity.cs","lineNumber":92,"sourceCode":"\n        // Calculate the intersection size of the two strings.\n        var intersectionSize = leftSet.Count + rightSet.Count - unionSet.Count;\n\n        // Return the Jaccard similarity coefficient as the ratio of intersection to union.\n        return 1.0d * intersectionSize / unionSet.Count;\n    }\n\n    /// <summary>\n    /// Validates the input strings and throws an exception if either is null.\n    /// </summary>\n    /// <param name=\"left\">The first string to validate.</param>\n    /// <param name=\"right\">The second string to validate.</param>\n    private void ValidateInput(string left, string right)\n    {\n        if (left == null || right == null)\n        {\n            var paramName = left == null ? nameof(left) : nameof(right);\n            throw new ArgumentNullException(paramName, \"Input cannot be null\");\n        }\n    }\n}\n","sourceCodeStart":74,"sourceCodeEnd":96,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Strings/Similarity/JaccardSimilarity.cs#L74-L96","documentation":"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.","triggerScenarios":"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).","commonSituations":"Nullable document/text fields from a database; deserialized objects with missing string properties; pipeline steps that emit null instead of empty content.","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."],"exampleFix":"// before\nvar score = jaccard.Calculate(docA.Text, docB.Text); // either may be null\n// after\nvar score = jaccard.Calculate(docA.Text ?? string.Empty, docB.Text ?? string.Empty);","handlingStrategy":"validation","validationCode":"if (left == null || right == null)\n{\n    // skip computation or coalesce: left ?? \"\" ; right ?? \"\"\n}","typeGuard":"static bool BothNonNull(string? a, string? b) => a != null && b != null;","tryCatchPattern":"try\n{\n    score = jaccard.Calculate(left, right);\n}\ncatch (ArgumentNullException ex)\n{\n    score = 0.0; // define similarity of null input as zero\n}","preventionTips":["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)."],"tags":["null-argument","argumentnull","jaccard","csharp","similarity"],"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"}