TheAlgorithms/Java · error · IllegalArgumentException

Unit cannot be null.

Error message

Unit cannot be null.

What it means

Thrown by TimeConverter.resolveUnit (called by convertTime) when either unitFrom or unitTo is null. The null check precedes the toLowerCase() call on the unit string. Supported units are: seconds, minutes, hours, days, weeks, months, years (case-insensitive).

Source

Thrown at src/main/java/com/thealgorithms/conversions/TimeConverter.java:89

     * @throws IllegalArgumentException if either {@code unitFrom} or {@code unitTo} is not supported
     */
    public static double convertTime(double timeValue, String unitFrom, String unitTo) {
        if (timeValue < 0) {
            throw new IllegalArgumentException("timeValue must be a non-negative number.");
        }

        TimeUnit from = resolveUnit(unitFrom);
        TimeUnit to = resolveUnit(unitTo);

        double secondsValue = from.toSeconds(timeValue);
        double converted = to.fromSeconds(secondsValue);

        return Math.round(converted * 1000.0) / 1000.0;
    }

    private static TimeUnit resolveUnit(String unit) {
        if (unit == null) {
            throw new IllegalArgumentException("Unit cannot be null.");
        }
        TimeUnit resolved = UNIT_LOOKUP.get(unit.toLowerCase(Locale.ROOT));
        if (resolved == null) {
            throw new IllegalArgumentException("Invalid unit '" + unit + "'. Supported units are: " + UNIT_LOOKUP.keySet());
        }
        return resolved;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check for null unit strings before calling convertTime and provide a default or error.
  2. If reading from a config file, validate that both unit keys are present at startup.
  3. Define unit constants or an enum and use those instead of free-form strings to eliminate null risk.

Example fix

// before
double result = TimeConverter.convertTime(timeValue, unitFrom, unitTo);

// after
if (unitFrom == null || unitTo == null) {
    throw new IllegalArgumentException("Unit strings must not be null");
}
double result = TimeConverter.convertTime(timeValue, unitFrom, unitTo);
Defensive patterns

Strategy: validation

Validate before calling

if (unitFrom == null || unitTo == null) {
    throw new IllegalArgumentException("Unit strings must not be null");
}
double result = TimeConverter.convertTime(timeValue, unitFrom, unitTo);

Type guard

static boolean areValidUnits(String from, String to) {
    return from != null && to != null;
}

Try / catch

try {
    double result = TimeConverter.convertTime(timeValue, unitFrom, unitTo);
} catch (IllegalArgumentException e) {
    // null or unsupported unit; use defaults or log
    logger.warn("Invalid time unit: from={}, to={}", unitFrom, unitTo);
}

Prevention

When it happens

Trigger: Calling convertTime(timeValue, null, "seconds") or convertTime(timeValue, "hours", null). Passing a unit string from a config or API where the key was missing and resolved to null. Supplying a variable that was not populated from a data source.

Common situations: A configuration property for the time unit is absent. A JSON field for unit designation is missing. A function parameter is conditionally set but the condition was not met.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/7ac330c124293571. Report an issue: GitHub.