chinabugotech/hutool · error · IllegalArgumentException

Date and Patterns must not be null

Error message

Date and Patterns must not be null

What it means

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.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java:793

	/**
	 * 通过给定的日期格式解析日期时间字符串。<br>
	 * 传入的日期格式会逐个尝试,直到解析成功,返回{@link Calendar}对象,否则抛出{@link DateException}异常。
	 * 方法来自:Apache Commons-Lang3
	 *
	 * @param str           日期时间字符串,非空
	 * @param locale        地区,当为{@code null}时使用{@link Locale#getDefault()}
	 * @param lenient       日期时间解析是否使用严格模式
	 * @param parsePatterns 需要尝试的日期时间格式数组,非空, 见SimpleDateFormat
	 * @return 解析后的Calendar
	 * @throws IllegalArgumentException if the date string or pattern array is null
	 * @throws DateException            if none of the date patterns were suitable
	 * @see java.util.Calendar#isLenient()
	 * @since 5.3.11
	 */
	public static Calendar parseByPatterns(String str, Locale locale, boolean lenient, String... parsePatterns) throws DateException {
		if (str == null || parsePatterns == null) {
			throw new IllegalArgumentException("Date and Patterns must not be null");
		}

		final TimeZone tz = TimeZone.getDefault();
		final Locale lcl = ObjectUtil.defaultIfNull(locale, Locale.getDefault());
		final ParsePosition pos = new ParsePosition(0);
		final Calendar calendar = Calendar.getInstance(tz, lcl);
		calendar.setLenient(lenient);

		for (final String parsePattern : parsePatterns) {
			if (GlobalCustomFormat.isCustomFormat(parsePattern)) {
				final Date parse = GlobalCustomFormat.parse(str, parsePattern);
				if (null == parse) {
					continue;
				}
				calendar.setTime(parse);
				return calendar;
			}

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Null-check str before calling: if (str != null) { parseByPatterns(str, patterns); }.
  2. Ensure parsePatterns is a non-null array; initialize defaults: String[] patterns = patternsFromConfig != null ? patternsFromConfig : new String[]{"yyyy-MM-dd"}.
  3. Avoid passing null as a varargs argument; pass an explicit empty array or specific patterns.
  4. Use ObjectUtil.defaultIfNull or Optional to provide fallbacks for both str and patterns.

Example fix

// before
Calendar cal = CalendarUtil.parseByPatterns(dateStr, patterns); // throws if either null

// after
if (dateStr != null && patterns != null && patterns.length > 0) {
    Calendar cal = CalendarUtil.parseByPatterns(dateStr, patterns);
}
Defensive patterns

Strategy: validation

Validate before calling

if (str == null || parsePatterns == null || parsePatterns.length == 0) {
    throw new IllegalArgumentException("str and patterns must be non-null and non-empty");
}
CalendarUtil.parseByPatterns(str, locale, lenient, parsePatterns);

Type guard

static boolean hasValidParseInputs(String str, String[] patterns) {
    return str != null && patterns != null && patterns.length > 0;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/ef7fde075a36c1d9. Report an issue: GitHub.