{"record":{"id":"ef7fde075a36c1d9","repo":"chinabugotech/hutool","slug":"date-and-patterns-must-not-be-null","errorCode":null,"errorMessage":"Date and Patterns must not be null","messagePattern":"Date and Patterns must not be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java","lineNumber":793,"sourceCode":"\n\t/**\n\t * 通过给定的日期格式解析日期时间字符串。<br>\n\t * 传入的日期格式会逐个尝试，直到解析成功，返回{@link Calendar}对象，否则抛出{@link DateException}异常。\n\t * 方法来自：Apache Commons-Lang3\n\t *\n\t * @param str           日期时间字符串，非空\n\t * @param locale        地区，当为{@code null}时使用{@link Locale#getDefault()}\n\t * @param lenient       日期时间解析是否使用严格模式\n\t * @param parsePatterns 需要尝试的日期时间格式数组，非空, 见SimpleDateFormat\n\t * @return 解析后的Calendar\n\t * @throws IllegalArgumentException if the date string or pattern array is null\n\t * @throws DateException            if none of the date patterns were suitable\n\t * @see java.util.Calendar#isLenient()\n\t * @since 5.3.11\n\t */\n\tpublic static Calendar parseByPatterns(String str, Locale locale, boolean lenient, String... parsePatterns) throws DateException {\n\t\tif (str == null || parsePatterns == null) {\n\t\t\tthrow new IllegalArgumentException(\"Date and Patterns must not be null\");\n\t\t}\n\n\t\tfinal TimeZone tz = TimeZone.getDefault();\n\t\tfinal Locale lcl = ObjectUtil.defaultIfNull(locale, Locale.getDefault());\n\t\tfinal ParsePosition pos = new ParsePosition(0);\n\t\tfinal Calendar calendar = Calendar.getInstance(tz, lcl);\n\t\tcalendar.setLenient(lenient);\n\n\t\tfor (final String parsePattern : parsePatterns) {\n\t\t\tif (GlobalCustomFormat.isCustomFormat(parsePattern)) {\n\t\t\t\tfinal Date parse = GlobalCustomFormat.parse(str, parsePattern);\n\t\t\t\tif (null == parse) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tcalendar.setTime(parse);\n\t\t\t\treturn calendar;\n\t\t\t}\n","sourceCodeStart":775,"sourceCodeEnd":811,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java#L775-L811","documentation":"Thrown by CalendarUtil.parseByPatterns() when the date string (str) is null OR the patterns array (parsePatterns) is null. Note: because parsePatterns is a varargs parameter, calling parseByPatterns(\"someString\") with no patterns yields an empty array, not null — but an explicit null passed as the array triggers this. This is a hard null precondition.","triggerScenarios":"Calling CalendarUtil.parseByPatterns(null, \"yyyy-MM-dd\"). Calling CalendarUtil.parseByPatterns(str, (String[]) null). Passing a null String array variable as parsePatterns. A varargs call where the only argument is null: parseByPatterns(\"str\", null) may pass null as the array.","commonSituations":"Date string from a nullable field or external API response that was not null-checked. Configuration loading where the patterns array comes from a properties file that may be absent. Varargs edge case: passing a single null argument which Java interprets as the array itself being null.","solutions":["Null-check str before calling: if (str != null) { parseByPatterns(str, patterns); }.","Ensure parsePatterns is a non-null array; initialize defaults: String[] patterns = patternsFromConfig != null ? patternsFromConfig : new String[]{\"yyyy-MM-dd\"}.","Avoid passing null as a varargs argument; pass an explicit empty array or specific patterns.","Use ObjectUtil.defaultIfNull or Optional to provide fallbacks for both str and patterns."],"exampleFix":"// before\nCalendar cal = CalendarUtil.parseByPatterns(dateStr, patterns); // throws if either null\n\n// after\nif (dateStr != null && patterns != null && patterns.length > 0) {\n    Calendar cal = CalendarUtil.parseByPatterns(dateStr, patterns);\n}","handlingStrategy":"validation","validationCode":"if (str == null || parsePatterns == null || parsePatterns.length == 0) {\n    throw new IllegalArgumentException(\"str and patterns must be non-null and non-empty\");\n}\nCalendarUtil.parseByPatterns(str, locale, lenient, parsePatterns);","typeGuard":"static boolean hasValidParseInputs(String str, String[] patterns) {\n    return str != null && patterns != null && patterns.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Null-check str and parsePatterns before calling parseByPatterns().","Never pass null as a varargs argument; use explicit non-null arrays.","Initialize pattern arrays with defaults from configuration."],"tags":["date","calendar","parse","null-check","patterns","argument-validation"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}