{"record":{"id":"68d7a8a56cf6741a","repo":"TheAlgorithms/C-Sharp","slug":"input-string-cannot-be-null","errorCode":null,"errorMessage":"Input string cannot be null.","messagePattern":"Input string cannot be null\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Strings/ManachersAlgorithm.cs","lineNumber":69,"sourceCode":"    /// <returns>The longest palindromic substring found in the input.</returns>\n    /// <exception cref=\"ArgumentException\">Thrown when the input string is null.</exception>\n    /// <example>\n    ///     Input: \"babad\".\n    ///     Output: \"bab\" or \"aba\" (both are valid longest palindromes with length 3).\n    ///\n    ///     Detailed Example:\n    ///     Input: \"abaxyz\".\n    ///     Transformed: \"^#a#b#a#x#y#z#$\".\n    ///     Process finds \"aba\" at indices 1-3 with radius 3 in transformed string.\n    ///     Maps back to indices 0-2 in original string.\n    ///     Output: \"aba\".\n    /// </example>\n    public static string FindLongestPalindrome(string input)\n    {\n        // Validate input\n        if (input == null)\n        {\n            throw new ArgumentException(\"Input string cannot be null.\", nameof(input));\n        }\n\n        // Handle edge cases\n        if (input.Length == 0)\n        {\n            return string.Empty;\n        }\n\n        if (input.Length == 1)\n        {\n            return input;\n        }\n\n        // STEP 1: Transform the string to handle even-length palindromes uniformly\n        // Example: \"abc\" becomes \"^#a#b#c#$\"\n        //\n        // WHY THIS WORKS:\n        // - Original \"aba\" (odd): Center is 'b' at index 1.","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Strings/ManachersAlgorithm.cs#L51-L87","documentation":"ManachersAlgorithm.FindLongestPalindrome validates that the input string is not null before running Manacher's algorithm, throwing ArgumentException with paramName \"input\". An empty string is explicitly allowed and returns string.Empty; only null is rejected. The library throws early so the O(n) algorithm never dereferences null.","triggerScenarios":"Calling FindLongestPalindrome(null) directly; passing a string variable that was never initialized or the result of an API that returned null.","commonSituations":"Database/text fields that are NULL rather than empty; deserialized JSON where the property was absent; chaining string operations where an earlier step produced null.","solutions":["Coalesce null to empty before the call: FindLongestPalindrome(input ?? string.Empty).","Add a null check at the call site and skip/return early when input is null.","Fix the upstream producer so it returns string.Empty instead of null for missing text.","Catch ArgumentException if null input is expected and handle it explicitly."],"exampleFix":"// before\nvar longest = ManachersAlgorithm.FindLongestPalindrome(text); // text may be null\n// after\nvar longest = ManachersAlgorithm.FindLongestPalindrome(text ?? string.Empty);","handlingStrategy":"type-guard","validationCode":"if (input == null)\n{\n    input = string.Empty; // or skip the call\n}","typeGuard":"static bool HasText(string? s) => s != null;","tryCatchPattern":"try\n{\n    var longest = ManachersAlgorithm.FindLongestPalindrome(input);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"input\")\n{\n    longest = string.Empty; // define null semantics explicitly\n}","preventionTips":["Enable <Nullable>enable</Nullable> so nullability is tracked at compile time.","Coalesce DB/JSON nulls to string.Empty at deserialization boundaries.","Never let uninitialized string fields flow into algorithm calls.","Document and test the null behavior of text-processing helpers."],"tags":["null-argument","argument-validation","csharp","palindrome"],"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"}