{"record":{"id":"9889b3b592c76fcd","repo":"TheAlgorithms/Java","slug":"input-string-cannot-be-null-or-empty-9889b3","errorCode":null,"errorMessage":"Input string cannot be null or empty","messagePattern":"Input string cannot be null or empty","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/HarshadNumber.java","lineNumber":57,"sourceCode":"\n        return n % sumOfDigits == 0;\n    }\n\n    /**\n     * Checks if a number represented as a string is a Harshad number.\n     * A Harshad number is a positive integer that is divisible by the sum of its\n     * digits.\n     *\n     * @param s the string representation of the number to be checked\n     * @return {@code true} if the number is a Harshad number, otherwise\n     *         {@code false}\n     * @throws IllegalArgumentException if {@code s} is null, empty, or represents a\n     *                                  non-positive integer\n     * @throws NumberFormatException    if {@code s} cannot be parsed as a long\n     */\n    public static boolean isHarshad(String s) {\n        if (s == null || s.isEmpty()) {\n            throw new IllegalArgumentException(\"Input string cannot be null or empty\");\n        }\n\n        final long n;\n        try {\n            n = Long.parseLong(s);\n        } catch (NumberFormatException e) {\n            throw new IllegalArgumentException(\"Input string must be a valid integer: \" + s, e);\n        }\n\n        if (n <= 0) {\n            throw new IllegalArgumentException(\"Input must be a positive integer. Received: \" + n);\n        }\n\n        int sumOfDigits = 0;\n        for (char ch : s.toCharArray()) {\n            if (Character.isDigit(ch)) {\n                sumOfDigits += ch - '0';\n            }","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/HarshadNumber.java#L39-L75","documentation":"Thrown by HarshadNumber.isHarshad(String s) when s is null or empty (s.isEmpty()). This is the string-overload variant of the Harshad checker that parses the string to a long. The null/empty guard runs before Long.parseLong to avoid a NullPointerException or NumberFormatException on unparseable input.","triggerScenarios":"Calling isHarshad((String) null), isHarshad(\"\"), or isHarshad(new String()). A string containing only whitespace (e.g., \" \") does NOT trigger this — it falls through to the NumberFormatException handler (error 413).","commonSituations":"User input fields that are blank or unsubmitted. JSON/XML deserialization producing null string fields. Data from APIs or files with missing values represented as null or empty strings.","solutions":["Check for null and empty before calling isHarshad(String).","Use a safe-input wrapper: if (s == null || s.isBlank()) return false.","Validate string inputs at the data-ingestion boundary."],"exampleFix":"// before\nboolean result = HarshadNumber.isHarshad(userInput);\n\n// after\nif (userInput == null || userInput.isBlank()) {\n    return false;\n}\nboolean result = HarshadNumber.isHarshad(userInput.trim());","handlingStrategy":"validation","validationCode":"if (s == null || s.isEmpty()) {\n    return false;\n}\nboolean result = HarshadNumber.isHarshad(s);","typeGuard":"static boolean isNonEmptyString(String s) {\n    return s != null && !s.isEmpty();\n}","tryCatchPattern":"try {\n    boolean result = HarshadNumber.isHarshad(s);\n} catch (IllegalArgumentException e) {\n    // s was null or empty; handle gracefully\n}","preventionTips":["Check for null and empty strings before calling.","Validate string inputs at the data-ingestion boundary.","Use s.isBlank() to also catch whitespace-only strings proactively."],"tags":["math","harshad","string-parsing","null-check","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}