{"record":{"id":"20cd7e2d4421b66c","repo":"TheAlgorithms/Java","slug":"input-cannot-be-null-20cd7e","errorCode":null,"errorMessage":"Input cannot be null","messagePattern":"Input cannot be null","errorType":"validation","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/RomanToInteger.java","lineNumber":73,"sourceCode":"     * Converts a Roman numeral string to its integer equivalent.\n     * Steps:\n     * <ol>\n     *     <li>Iterate over the string from right to left.</li>\n     *     <li>For each character, convert it to an integer value.</li>\n     *     <li>If the current value is greater than or equal to the max previous value, add it.</li>\n     *     <li>Otherwise, subtract it from the sum.</li>\n     *     <li>Update the max previous value.</li>\n     *     <li>Return the sum.</li>\n     * </ol>\n     *\n     * @param roman the Roman numeral string\n     * @return the integer value of the Roman numeral\n     * @throws IllegalArgumentException if the input contains invalid Roman characters\n     * @throws NullPointerException if the input is {@code null}\n     */\n    public static int romanToInt(String roman) {\n        if (roman == null) {\n            throw new NullPointerException(\"Input cannot be null\");\n        }\n\n        roman = roman.toUpperCase();\n        int sum = 0;\n        int maxPrevValue = 0;\n        for (int i = roman.length() - 1; i >= 0; i--) {\n            int currentValue = romanSymbolToInt(roman.charAt(i));\n            if (currentValue >= maxPrevValue) {\n                sum += currentValue;\n                maxPrevValue = currentValue;\n            } else {\n                sum -= currentValue;\n            }\n        }\n\n        return sum;\n    }\n}","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/RomanToInteger.java#L55-L91","documentation":"Thrown by RomanToInteger.romanToInt as a NullPointerException when the input string is null. Note: this is a NullPointerException, not an IllegalArgumentException — the Javadoc explicitly documents this. The check precedes the toUpperCase() call, which would otherwise throw its own NullPointerException.","triggerScenarios":"Calling romanToInt(null). Passing a variable that was not populated from a config, database, or API response. Supplying a null field from a JSON deserialization where the key was absent.","commonSituations":"A database column or config property for a Roman numeral is null. A JSON field is missing and the deserializer maps it to null. A function parameter is conditionally set but the condition was not met.","solutions":["Check for null before calling romanToInt and handle the missing-value case.","Use Objects.requireNonNull(roman, \"roman numeral input\") if null should be treated as a programming error.","If null should map to zero, check and return 0 yourself before calling the method."],"exampleFix":"// before\nint result = RomanToInteger.romanToInt(romanStr); // romanStr may be null\n\n// after\nif (romanStr == null) {\n    throw new IllegalArgumentException(\"Roman numeral input must not be null\");\n    // or: return 0; // if null should mean zero\n}\nint result = RomanToInteger.romanToInt(romanStr);","handlingStrategy":"validation","validationCode":"if (roman == null) {\n    throw new IllegalArgumentException(\"Roman numeral input must not be null\");\n}\nint result = RomanToInteger.romanToInt(roman);","typeGuard":"static boolean isNotNullRoman(String s) {\n    return s != null;\n}","tryCatchPattern":"try {\n    int result = RomanToInteger.romanToInt(roman);\n} catch (NullPointerException e) {\n    // input was null; provide a default or log the error\n    logger.warn(\"Null Roman numeral input\");\n}","preventionTips":["Note that this throws NullPointerException, not IllegalArgumentException — catch the correct type.","Use Objects.requireNonNull(roman, \"message\") if null is a programming error, not a user error.","Validate all string inputs for null at the data boundary."],"tags":["roman-numerals","input-validation","null-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}