alibaba/nacos · warning · NullPointerException

date must not be null

Error message

date must not be null

What it means

Thrown by DateFormatUtils.format(Date, String) when the date argument is null. The method explicitly checks and throws NullPointerException (not IllegalArgumentException). The Javadoc declares both date and pattern must be non-null.

Source

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

    
    public static final String MM = "MM";
    
    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. Null-check the Date before formatting.
  2. Fix the upstream source so the Date is never null (require it, or default to a sentinel).
  3. Return a default formatted string or skip formatting when the date is absent.

Example fix

// before
String s = DateFormatUtils.format(maybeNullDate, DateFormatUtils.YYYYMMDDHHMMSSNOMARK);

// after
String s = maybeNullDate == null ? null : DateFormatUtils.format(maybeNullDate, DateFormatUtils.YYYYMMDDHHMMSSNOMARK);
Defensive patterns

Strategy: validation

Validate before calling

if (date == null) {
    return null; // or throw domain error
}
return DateFormatUtils.format(date, pattern);

Prevention

When it happens

Trigger: Calling DateFormatUtils.format(nullDate, pattern) where nullDate is a Date that was never assigned (e.g., a parsed date that failed to populate, a DB column that was null).

Common situations: A nullable timestamp field from a database row or external payload passed straight to format(); a date parse upstream that returned null on failure and was not checked.

Related errors


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