TheAlgorithms/Java · error · IllegalArgumentException
Input expression cannot be null.
Error message
Input expression cannot be null.
What it means
DuplicateBrackets.check(String) scans an arithmetic/expression string for redundant brackets (e.g. `(a)` or `((a))`). A null expression cannot be iterated and is rejected early with IllegalArgumentException rather than throwing NullPointerException at charAt/expression.length().
Source
Thrown at src/main/java/com/thealgorithms/stacks/DuplicateBrackets.java:22
/**
* Class for detecting unnecessary or redundant brackets in a mathematical expression.
* Assumes the expression is balanced (i.e., all opening brackets have matching closing brackets).
*/
public final class DuplicateBrackets {
private DuplicateBrackets() {
}
/**
* Checks for extra or redundant brackets in a given expression.
*
* @param expression the string representing the expression to be checked
* @return true if there are extra or redundant brackets, false otherwise
* @throws IllegalArgumentException if the input string is null
*/
public static boolean check(String expression) {
if (expression == null) {
throw new IllegalArgumentException("Input expression cannot be null.");
}
Stack<Character> stack = new Stack<>();
for (int i = 0; i < expression.length(); i++) {
char ch = expression.charAt(i);
if (ch == ')') {
if (stack.isEmpty() || stack.peek() == '(') {
return true;
}
while (!stack.isEmpty() && stack.peek() != '(') {
stack.pop();
}
if (!stack.isEmpty()) {
stack.pop();
}
} else {
stack.push(ch);
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass an empty string "" instead of null when there is no expression.
- Null-check before calling and skip/short-circuit the check.
- Sanitize input at the boundary so null never reaches this method.
Example fix
// before boolean redundant = DuplicateBrackets.check(expr); // expr may be null // after boolean redundant = expr != null && DuplicateBrackets.check(expr); // or normalize: DuplicateBrackets.check(expr == null ? "" : expr);
Defensive patterns
Strategy: validation
Validate before calling
static boolean safeCheck(String expression) {
if (expression == null) {
return false; // null has no redundant brackets
}
return DuplicateBrackets.check(expression);
} Type guard
static boolean isCheckableExpression(String s) {
return s != null;
} Prevention
- Default optional expression fields to "" rather than null.
- Guard parser/reader output that can yield null on malformed input.
- Normalize null at the input boundary so it never propagates downstream.
When it happens
Trigger: Calling `DuplicateBrackets.check(null)`, or passing an expression variable sourced from a nullable field, Map.get, or a parser that returns null on malformed input.
Common situations: Optional expression fields omitted in config; reading expressions from files/stdin where an empty line was normalized to null; refactors that dropped a default value; JSON deserialization producing null for absent fields.
Related errors
- brackets is 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/71ceb79692dce2f0.
Report an issue: GitHub.