{"record":{"id":"7ac330c124293571","repo":"TheAlgorithms/Java","slug":"unit-cannot-be-null","errorCode":null,"errorMessage":"Unit cannot be null.","messagePattern":"Unit cannot be null\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/TimeConverter.java","lineNumber":89,"sourceCode":"     * @throws IllegalArgumentException if either {@code unitFrom} or {@code unitTo} is not supported\n     */\n    public static double convertTime(double timeValue, String unitFrom, String unitTo) {\n        if (timeValue < 0) {\n            throw new IllegalArgumentException(\"timeValue must be a non-negative number.\");\n        }\n\n        TimeUnit from = resolveUnit(unitFrom);\n        TimeUnit to = resolveUnit(unitTo);\n\n        double secondsValue = from.toSeconds(timeValue);\n        double converted = to.fromSeconds(secondsValue);\n\n        return Math.round(converted * 1000.0) / 1000.0;\n    }\n\n    private static TimeUnit resolveUnit(String unit) {\n        if (unit == null) {\n            throw new IllegalArgumentException(\"Unit cannot be null.\");\n        }\n        TimeUnit resolved = UNIT_LOOKUP.get(unit.toLowerCase(Locale.ROOT));\n        if (resolved == null) {\n            throw new IllegalArgumentException(\"Invalid unit '\" + unit + \"'. Supported units are: \" + UNIT_LOOKUP.keySet());\n        }\n        return resolved;\n    }\n}\n","sourceCodeStart":71,"sourceCodeEnd":98,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/TimeConverter.java#L71-L98","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check for null unit strings before calling convertTime and provide a default or error.","If reading from a config file, validate that both unit keys are present at startup.","Define unit constants or an enum and use those instead of free-form strings to eliminate null risk."],"exampleFix":"// before\ndouble result = TimeConverter.convertTime(timeValue, unitFrom, unitTo);\n\n// after\nif (unitFrom == null || unitTo == null) {\n    throw new IllegalArgumentException(\"Unit strings must not be null\");\n}\ndouble result = TimeConverter.convertTime(timeValue, unitFrom, unitTo);","handlingStrategy":"validation","validationCode":"if (unitFrom == null || unitTo == null) {\n    throw new IllegalArgumentException(\"Unit strings must not be null\");\n}\ndouble result = TimeConverter.convertTime(timeValue, unitFrom, unitTo);","typeGuard":"static boolean areValidUnits(String from, String to) {\n    return from != null && to != null;\n}","tryCatchPattern":"try {\n    double result = TimeConverter.convertTime(timeValue, unitFrom, unitTo);\n} catch (IllegalArgumentException e) {\n    // null or unsupported unit; use defaults or log\n    logger.warn(\"Invalid time unit: from={}, to={}\", unitFrom, unitTo);\n}","preventionTips":["Define unit strings as constants or use an enum to eliminate null and typo risks.","Validate config properties for time units at application startup.","The supported units are: seconds, minutes, hours, days, weeks, months, years (case-insensitive)."],"tags":["time-conversion","input-validation","null-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}