TheAlgorithms/C-Sharp · error

The input expression cannot be null or empty.

Error message

The input expression cannot be null or empty.

What it means

BalancedParenthesesChecker.IsBalanced throws ArgumentException("The input expression cannot be null or empty.") when the expression string is null or the empty string. There is nothing to validate in such input, and the library treats it as an invalid argument rather than silently returning true/false.

Solutions

  1. Check string.IsNullOrWhiteSpace(expression) before calling and decide the desired semantics (treat empty as balanced = true, or report a validation error).
  2. Sanitize input earlier in the pipeline so the checker only receives non-empty parenthesis strings.
  3. Catch ArgumentException at the boundary and convert to your own validation message for the user.
  4. Provide a sensible default expression when the input source is optional.

Example fix

// before
bool ok = checker.IsBalanced(userInput);
// after
bool ok = string.IsNullOrWhiteSpace(userInput) ? true : checker.IsBalanced(userInput);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(expression)) return true; // or your chosen empty-input semantics

Type guard

bool HasExpression(string? s) => !string.IsNullOrWhiteSpace(s);

Try / catch

try { return checker.IsBalanced(expression); }
catch (ArgumentException ex) when (ex.Message.Contains("null or empty")) { return true; }

Prevention

When it happens

Trigger: IsBalanced(null) or IsBalanced("") — passing user input that was never entered, an empty config value, or a string trimmed down to nothing before the call.

Common situations: Form fields submitted empty; file/line reads yielding empty strings; optional settings defaulting to null; pipelines where an upstream filter removed all characters (e.g. stripped non-parenthesis chars leaving "").

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at Algorithms/Stack/BalancedParenthesesChecker.cs:35

    /// Determines if a given string expression containing brackets is balanced.
    /// A string is considered balanced if all opening brackets have corresponding closing brackets
    /// in the correct order. The supported brackets are '()', '{}', and '[]'.
    /// </summary>
    /// <param name="expression">
    /// The input string expression containing the brackets to check for balance.
    /// </param>
    /// <returns>
    /// <c>true</c> if the brackets in the expression are balanced; otherwise, <c>false</c>.
    /// </returns>
    /// <exception cref="ArgumentException">
    /// Thrown when the input expression contains invalid characters or is null/empty.
    /// Only '(', ')', '{', '}', '[', ']' characters are allowed.
    /// </exception>
    public bool IsBalanced(string expression)
    {
        if (string.IsNullOrEmpty(expression))
        {
            throw new ArgumentException("The input expression cannot be null or empty.");
        }

        Stack<char> stack = new Stack<char>();
        foreach (char c in expression)
        {
            if (IsOpeningParenthesis(c))
            {
                stack.Push(c);
            }
            else if (IsClosingParenthesis(c))
            {
                if (!IsBalancedClosing(stack, c))
                {
                    return false;
                }
            }
            else
            {

View on GitHub (pinned to 96e2905cab)