TheAlgorithms/C-Sharp · error · ArgumentException

Input string cannot be null.

Error message

Input string cannot be null.

What it means

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.

Solutions

  1. Coalesce null to empty before the call: FindLongestPalindrome(input ?? string.Empty).
  2. Add a null check at the call site and skip/return early when input is null.
  3. Fix the upstream producer so it returns string.Empty instead of null for missing text.
  4. Catch ArgumentException if null input is expected and handle it explicitly.

Example fix

// before
var longest = ManachersAlgorithm.FindLongestPalindrome(text); // text may be null
// after
var longest = ManachersAlgorithm.FindLongestPalindrome(text ?? string.Empty);
Defensive patterns

Strategy: type-guard

Validate before calling

if (input == null)
{
    input = string.Empty; // or skip the call
}

Type guard

static bool HasText(string? s) => s != null;

Try / catch

try
{
    var longest = ManachersAlgorithm.FindLongestPalindrome(input);
}
catch (ArgumentException ex) when (ex.ParamName == "input")
{
    longest = string.Empty; // define null semantics explicitly
}

Prevention

When it happens

Trigger: Calling FindLongestPalindrome(null) directly; passing a string variable that was never initialized or the result of an API that returned null.

Common situations: 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.

Related errors


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

Appendix: source

Thrown at Algorithms/Strings/ManachersAlgorithm.cs:69

    /// <returns>The longest palindromic substring found in the input.</returns>
    /// <exception cref="ArgumentException">Thrown when the input string is null.</exception>
    /// <example>
    ///     Input: "babad".
    ///     Output: "bab" or "aba" (both are valid longest palindromes with length 3).
    ///
    ///     Detailed Example:
    ///     Input: "abaxyz".
    ///     Transformed: "^#a#b#a#x#y#z#$".
    ///     Process finds "aba" at indices 1-3 with radius 3 in transformed string.
    ///     Maps back to indices 0-2 in original string.
    ///     Output: "aba".
    /// </example>
    public static string FindLongestPalindrome(string input)
    {
        // Validate input
        if (input == null)
        {
            throw new ArgumentException("Input string cannot be null.", nameof(input));
        }

        // Handle edge cases
        if (input.Length == 0)
        {
            return string.Empty;
        }

        if (input.Length == 1)
        {
            return input;
        }

        // STEP 1: Transform the string to handle even-length palindromes uniformly
        // Example: "abc" becomes "^#a#b#c#$"
        //
        // WHY THIS WORKS:
        // - Original "aba" (odd): Center is 'b' at index 1.

View on GitHub (pinned to 96e2905cab)