alibaba/nacos · warning · NullPointerException

pattern must not be null

Error message

pattern must not be null

What it means

Thrown by DateFormatUtils.format(Date, String) when the pattern argument is null. Distinct from the date-null case (790); here the date is fine but the format pattern is missing.

Source

Thrown at common/src/main/java/com/alibaba/nacos/common/utils/DateFormatUtils.java:63

    public static final String DD = "dd";
    
    public static final String YYYYMMDDSLASH = "yyyy/MM/dd";
    
    public static final String YYYYMMDDHHMMSSNOMARK = "yyyyMMddHHmmss";
    
    /**
     * Formats a date/time into a specific pattern.
     *
     * @param date  the date to format, not null
     * @param pattern  the pattern to use to format the date, not null
     * @return the formatted date
     */
    public static String format(final Date date, final String pattern) {
        if (date == null) {
            throw new NullPointerException("date must not be null");
        }
        if (pattern == null) {
            throw new NullPointerException("pattern must not be null");
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
    
}

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Validate the pattern is non-null before formatting.
  2. Default the pattern to a known constant (e.g., DateFormatUtils.YYYYMMDDHHMMSSNOMARK).
  3. Surface configuration errors for the missing pattern key at startup.

Example fix

// before
String s = DateFormatUtils.format(date, patternFromConfig);

// after
String pattern = patternFromConfig != null ? patternFromConfig : DateFormatUtils.YYYYMMDDHHMMSSNOMARK;
String s = DateFormatUtils.format(date, pattern);
Defensive patterns

Strategy: validation

Validate before calling

if (pattern == null) {
    pattern = DateFormatUtils.YYYYMMDDHHMMSSNOMARK; // safe default
}
return DateFormatUtils.format(date, pattern);

Prevention

When it happens

Trigger: Calling DateFormatUtils.format(date, nullPattern) — pattern is a nullable variable or a constant that resolved to null.

Common situations: A pattern sourced from configuration that was not set; a conditional that leaves the pattern null on some code path; passing a user-supplied pattern without validation.

Related errors


AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14). Data as JSON: /api/errors/ab5e48295683da3b. Report an issue: GitHub.