TheAlgorithms/C-Sharp · error · ArgumentException

Infix cannot be null or empty.

Error message

Infix cannot be null or empty.

What it means

ValidateInfix is a private guard called by InfixToPostfixConversion; it throws ArgumentException when the infix expression string is null, empty, or whitespace-only, since there is nothing to convert.

Solutions

  1. Check string.IsNullOrWhiteSpace(expr) before calling InfixToPostfixConversion and skip or prompt for input.
  2. Provide a default expression when the input is empty.
  3. Catch ArgumentException and surface a 'please enter an expression' message.

Example fix

// before
string postfix = converter.InfixToPostfixConversion(userInput);
// after
if (!string.IsNullOrWhiteSpace(userInput))
{
    string postfix = converter.InfixToPostfixConversion(userInput);
}
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(infix)) return null; // or prompt for input before converting

Type guard

bool HasInfix(string s) => !string.IsNullOrWhiteSpace(s);

Try / catch

try { var postfix = converter.InfixToPostfixConversion(infix); } catch (ArgumentException) { /* treat empty input as no-op / user prompt */ }

Prevention

When it happens

Trigger: Calling InfixToPostfixConversion(null), InfixToPostfixConversion(""), or InfixToPostfixConversion(" ").

Common situations: Configuration values or user input fields left blank; upstream methods returning null that are passed straight through.

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/78387f8916b0d289. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Stack/InfixToPostfix.cs:184

            int result = op switch
            {
                '+' => a + b,
                '-' => a - b,
                '*' => a * b,
                '/' => a / b,
                '^' => (int)Math.Pow(a, b),
                _ => throw new InvalidOperationException($"Unknown operator {op}"),
            };

            stack.Push(result);
        }

        private static void ValidateInfix(string expr)
        {
            if (string.IsNullOrEmpty(expr) || string.IsNullOrWhiteSpace(expr))
            {
                throw new ArgumentException("Infix cannot be null or empty.");
            }
        }

        private static void ValidatePostfix(string expr)
        {
            if (string.IsNullOrEmpty(expr) || string.IsNullOrWhiteSpace(expr))
            {
                throw new ArgumentException("Postfix cannot be null or empty.");
            }
        }

        /// <summary>
        /// Decided Operator Precedence.
        /// <param name="operatorChar"> Operator character whose precedence is asked.</param>
        /// <returns>Precedence rank of parameter operator character.</returns>
        /// </summary>
        [ExcludeFromCodeCoverage]
        private static int Precedence(char operatorChar)

View on GitHub (pinned to 96e2905cab)