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
- Pass an empty string "" when no brackets are present instead of null.
- Null-check at the call site and treat null as 'no brackets' (return true or skip).
- 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
- Normalize null to "" at your system boundary.
- Use Objects.requireNonNullElse(s, "") when a default is acceptable.
- Never pass Map.get/JSON-field results straight into the method without a null guard.
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
- Input expression cannot be null.
- Input cannot be null
- Weights matrix must not be null or empty
- Weights matrix must be square
- X and Y must be non-null, non-empty, and of the same length.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/0e05151afa46a6bb.
Report an issue: GitHub.