TheAlgorithms/Java · error · IllegalArgumentException
Input cannot be null or empty
Error message
Input cannot be null or empty
What it means
Thrown by OctalToHexadecimal.octalToDecimal when the octalNumber string is null or empty. This guard precedes the digit-by-digit parsing loop; a null input would otherwise cause a NullPointerException and an empty input would silently return 0 without meaningful conversion.
Source
Thrown at src/main/java/com/thealgorithms/conversions/OctalToHexadecimal.java:25
*/
public final class OctalToHexadecimal {
private static final int OCTAL_BASE = 8;
private static final int HEX_BASE = 16;
private static final String HEX_DIGITS = "0123456789ABCDEF";
private OctalToHexadecimal() {
}
/**
* Converts an Octal number (as a string) to its Decimal equivalent.
*
* @param octalNumber The Octal number as a string
* @return The Decimal equivalent of the Octal number
* @throws IllegalArgumentException if the input contains invalid octal digits
*/
public static int octalToDecimal(String octalNumber) {
if (octalNumber == null || octalNumber.isEmpty()) {
throw new IllegalArgumentException("Input cannot be null or empty");
}
int decimalValue = 0;
for (int i = 0; i < octalNumber.length(); i++) {
char currentChar = octalNumber.charAt(i);
if (currentChar < '0' || currentChar > '7') {
throw new IllegalArgumentException("Incorrect octal digit: " + currentChar);
}
int currentDigit = currentChar - '0';
decimalValue = decimalValue * OCTAL_BASE + currentDigit;
}
return decimalValue;
}
/**
* Converts a Decimal number to its Hexadecimal equivalent.
*View on GitHub (pinned to fdfb9a395b)
Solutions
- Check for null or empty before calling octalToDecimal and handle the missing-value case.
- If null/empty should default to zero, handle that explicitly in the caller.
- Validate input at the data boundary — reject or normalize empty strings early.
Example fix
// before
int decimal = OctalToHexadecimal.octalToDecimal(octalStr);
// after
if (octalStr == null || octalStr.isEmpty()) {
throw new IllegalArgumentException("Octal input must not be null or empty");
}
int decimal = OctalToHexadecimal.octalToDecimal(octalStr); Defensive patterns
Strategy: validation
Validate before calling
if (octalNumber == null || octalNumber.isEmpty()) {
throw new IllegalArgumentException("Octal input must not be null or empty");
}
int result = OctalToHexadecimal.octalToDecimal(octalNumber); Type guard
static boolean isValidOctalString(String s) {
return s != null && !s.isEmpty() && s.matches("^[0-7]+$");
} Try / catch
try {
int result = OctalToHexadecimal.octalToDecimal(octalNumber);
} catch (IllegalArgumentException e) {
// null, empty, or invalid input; handle gracefully
logger.warn("Invalid octal input: {}", octalNumber);
} Prevention
- Check for null and empty in the same guard that validates digits.
- If null/empty should default to zero, handle it before calling the library.
- Validate at the data entry point to prevent propagation.
When it happens
Trigger: Calling octalToDecimal(null) or octalToDecimal(""). Passing a variable populated from a config key that is missing. Supplying an empty string from a form field or parsed data source.
Common situations: A configuration property for an octal value is absent. A database column is null for some records. A user submits a form without filling in the octal field.
Related errors
- Input cannot be null or empty
- Decimal number cannot be negative.
- Incorrect input: Expecting an octal number (digits 0-7)
- Incorrect octal digit: {}
- Base must be between 2 and 36
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/a758d937129fb259.
Report an issue: GitHub.