{"record":{"id":"71ceb79692dce2f0","repo":"TheAlgorithms/Java","slug":"input-expression-cannot-be-null","errorCode":null,"errorMessage":"Input expression cannot be null.","messagePattern":"Input expression cannot be null\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/stacks/DuplicateBrackets.java","lineNumber":22,"sourceCode":"\n/**\n * Class for detecting unnecessary or redundant brackets in a mathematical expression.\n * Assumes the expression is balanced (i.e., all opening brackets have matching closing brackets).\n */\npublic final class DuplicateBrackets {\n    private DuplicateBrackets() {\n    }\n\n    /**\n     * Checks for extra or redundant brackets in a given expression.\n     *\n     * @param expression the string representing the expression to be checked\n     * @return true if there are extra or redundant brackets, false otherwise\n     * @throws IllegalArgumentException if the input string is null\n     */\n    public static boolean check(String expression) {\n        if (expression == null) {\n            throw new IllegalArgumentException(\"Input expression cannot be null.\");\n        }\n\n        Stack<Character> stack = new Stack<>();\n        for (int i = 0; i < expression.length(); i++) {\n            char ch = expression.charAt(i);\n            if (ch == ')') {\n                if (stack.isEmpty() || stack.peek() == '(') {\n                    return true;\n                }\n                while (!stack.isEmpty() && stack.peek() != '(') {\n                    stack.pop();\n                }\n                if (!stack.isEmpty()) {\n                    stack.pop();\n                }\n            } else {\n                stack.push(ch);\n            }","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/stacks/DuplicateBrackets.java#L4-L40","documentation":"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().","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nboolean redundant = DuplicateBrackets.check(expr); // expr may be null\n\n// after\nboolean redundant = expr != null && DuplicateBrackets.check(expr);\n// or normalize:\nDuplicateBrackets.check(expr == null ? \"\" : expr);","handlingStrategy":"validation","validationCode":"static boolean safeCheck(String expression) {\n    if (expression == null) {\n        return false; // null has no redundant brackets\n    }\n    return DuplicateBrackets.check(expression);\n}","typeGuard":"static boolean isCheckableExpression(String s) {\n    return s != null;\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["stacks","brackets","null-check","validation","illegalargument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}