TheAlgorithms/Java · error · IllegalArgumentException

brackets is null

Error message

brackets is null

What it means

BalancedBrackets.isBalanced(String) checks whether a string of bracket characters is correctly matched and nested. A null input has no characters to inspect and would also NPE inside toCharArray(), so the method fails fast with an IllegalArgumentException naming the problem ('brackets is null') rather than an opaque NullPointerException.

Source

Thrown at src/main/java/com/thealgorithms/stacks/BalancedBrackets.java:55

        };
        for (char[] pairedBracket : pairedBrackets) {
            if (pairedBracket[0] == leftBracket && pairedBracket[1] == rightBracket) {
                return true;
            }
        }
        return false;
    }

    /**
     * Check if {@code brackets} is balanced
     *
     * @param brackets the brackets
     * @return {@code true} if {@code brackets} is balanced, otherwise
     * {@code false}
     */
    public static boolean isBalanced(String brackets) {
        if (brackets == null) {
            throw new IllegalArgumentException("brackets is null");
        }
        Stack<Character> bracketsStack = new Stack<>();
        for (char bracket : brackets.toCharArray()) {
            switch (bracket) {
            case '(':
            case '[':
            case '<':
            case '{':
                bracketsStack.push(bracket);
                break;
            case ')':
            case ']':
            case '>':
            case '}':
                if (bracketsStack.isEmpty() || !isPaired(bracketsStack.pop(), bracket)) {
                    return false;
                }
                break;

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass an empty string "" when no brackets are present instead of null.
  2. Null-check at the call site and treat null as 'no brackets' (return true or skip).
  3. Ensure the upstream source provides a non-null default.

Example fix

// before
boolean ok = BalancedBrackets.isBrackets(maybeNull);

// after
boolean ok = maybeNull == null || BalancedBrackets.isBalanced(maybeNull);
// or pass a default:
BalancedBrackets.isBalanced(maybeNull == null ? "" : maybeNull);
Defensive patterns

Strategy: validation

Validate before calling

static boolean safeIsBalanced(String brackets) {
    if (brackets == null) {
        return true; // or throw a domain-specific error
    }
    return BalancedBrackets.isBalanced(brackets);
}

Type guard

static boolean isCheckableString(String s) {
    return s != null;
}

Prevention

When it happens

Trigger: Calling `BalancedBrackets.isBalanced(null)` directly, or passing a variable that was never assigned / came from a Map.get returning null / a JSON field that was absent.

Common situations: Optional config fields parsed as null when omitted; reading from a source that returns null on missing data; refactors that removed an upstream default; lazy-initialized fields not yet populated.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/0e05151afa46a6bb. Report an issue: GitHub.