TheAlgorithms/Java · error · NullPointerException
Input cannot be null
Error message
Input cannot be null
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/conversions/RomanToInteger.java:73
* Converts a Roman numeral string to its integer equivalent.
* Steps:
* <ol>
* <li>Iterate over the string from right to left.</li>
* <li>For each character, convert it to an integer value.</li>
* <li>If the current value is greater than or equal to the max previous value, add it.</li>
* <li>Otherwise, subtract it from the sum.</li>
* <li>Update the max previous value.</li>
* <li>Return the sum.</li>
* </ol>
*
* @param roman the Roman numeral string
* @return the integer value of the Roman numeral
* @throws IllegalArgumentException if the input contains invalid Roman characters
* @throws NullPointerException if the input is {@code null}
*/
public static int romanToInt(String roman) {
if (roman == null) {
throw new NullPointerException("Input cannot be null");
}
roman = roman.toUpperCase();
int sum = 0;
int maxPrevValue = 0;
for (int i = roman.length() - 1; i >= 0; i--) {
int currentValue = romanSymbolToInt(roman.charAt(i));
if (currentValue >= maxPrevValue) {
sum += currentValue;
maxPrevValue = currentValue;
} else {
sum -= currentValue;
}
}
return sum;
}
}View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
int result = RomanToInteger.romanToInt(romanStr); // romanStr may be null
// after
if (romanStr == null) {
throw new IllegalArgumentException("Roman numeral input must not be null");
// or: return 0; // if null should mean zero
}
int result = RomanToInteger.romanToInt(romanStr); Defensive patterns
Strategy: validation
Validate before calling
if (roman == null) {
throw new IllegalArgumentException("Roman numeral input must not be null");
}
int result = RomanToInteger.romanToInt(roman); Type guard
static boolean isNotNullRoman(String s) {
return s != null;
} Try / catch
try {
int result = RomanToInteger.romanToInt(roman);
} catch (NullPointerException e) {
// input was null; provide a default or log the error
logger.warn("Null Roman numeral input");
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- IPv4 address is empty.
- Input cannot be null or empty
- Input cannot be null or empty
- Unknown Roman symbol: {}
- Unit cannot be null.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/20cd7e2d4421b66c.
Report an issue: GitHub.