TheAlgorithms/C-Sharp · error · ArgumentException

Postfix cannot be null or empty.

Error message

Postfix cannot be null or empty.

What it means

InfixToPostfix.ValidatePostfix guards the ConvertToPostfix/Evaluation pipeline by rejecting a null, empty, or whitespace-only postfix expression string. The library throws ArgumentException early so downstream parsing/evaluation code never operates on meaningless input. It is an input-validation failure, not an algorithmic one.

Solutions

  1. Ensure the caller supplies a non-empty, non-whitespace postfix expression (e.g. "3 4 +") before invoking the API.
  2. Add a guard at the call site: if (string.IsNullOrWhiteSpace(expr)) return/handle before calling PostfixExpressionEvaluation.
  3. If the expression comes from user input or config, validate and prompt/fix the source of the empty value.
  4. Catch ArgumentException if empty input is a legitimate runtime condition and surface a friendly message.

Example fix

// before
var result = PostfixExpressionEvaluation(userInput); // userInput may be null/blank
// after
var result = string.IsNullOrWhiteSpace(userInput)
    ? throw new FormatException("Please enter an expression.")
    : PostfixExpressionEvaluation(userInput);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(expr))
{
    // handle: prompt user, use default, or return early
    return;
}

Try / catch

try
{
    var result = PostfixExpressionEvaluation(expr);
}
catch (ArgumentException ex) when (ex.Message.Contains("Postfix cannot be null or empty"))
{
    // surface a friendly 'expression is required' message
}

Prevention

When it happens

Trigger: Calling the public postfix evaluation/convert API (via PostfixExpressionEvaluation) with null, "", or a string of only spaces/tabs, e.g. PostfixExpressionEvaluation(null) or PostfixExpressionEvaluation(" ").

Common situations: Reading the expression from a config key, env var, or user textbox that was never filled in; splitting a line of input where the expression column is missing; passing a variable that was declared but never assigned.

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/4071e20f3cde8fed. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Stack/InfixToPostfix.cs:192

                _ => 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)
        {
            if (operatorChar == '^')
            {
                return 3;
            }

            if (operatorChar == '*' || operatorChar == '/')
            {

View on GitHub (pinned to 96e2905cab)