chinabugotech/hutool · error · NullPointerException
Date to compare is null !
Error message
Date to compare is null !
What it means
Thrown by DateTime.isBefore(Date) when the date argument is null. Unlike java.util.Date's natural behavior (which would NPE later), Hutool checks explicitly and throws NullPointerException with a descriptive message. The method delegates to compareTo(date), so a null argument is caught at the guard.
Source
Thrown at hutool-core/src/main/java/cn/hutool/core/date/DateTime.java:801
*/
public boolean isIn(Date beginDate, Date endDate) {
long beginMills = beginDate.getTime();
long endMills = endDate.getTime();
long thisMills = this.getTime();
return thisMills >= Math.min(beginMills, endMills) && thisMills <= Math.max(beginMills, endMills);
}
/**
* 是否在给定日期之前
*
* @param date 日期
* @return 是否在给定日期之前
* @since 4.1.3
*/
public boolean isBefore(Date date) {
if (null == date) {
throw new NullPointerException("Date to compare is null !");
}
return compareTo(date) < 0;
}
/**
* 是否在给定日期之前或与给定日期相等
*
* @param date 日期
* @return 是否在给定日期之前或与给定日期相等
* @since 3.0.9
*/
public boolean isBeforeOrEquals(Date date) {
if (null == date) {
throw new NullPointerException("Date to compare is null !");
}
return compareTo(date) <= 0;
}
View on GitHub (pinned to 8870454b2a)
Solutions
- Null-check the argument: if (date != null) dateTime.isBefore(date).
- Use Objects.requireNonNullElse(date, fallback) to provide a default.
- Wrap in a null-safe comparison utility that returns false or Optional when the argument is null.
- Validate upstream: ensure date sources never produce null.
Example fix
// before boolean before = dateTime.isBefore(otherDate); // throws if null // after boolean before = otherDate != null && dateTime.isBefore(otherDate);
Defensive patterns
Strategy: validation
Validate before calling
if (date == null) {
return false; // or handle per business rule
}
dateTime.isBefore(date); Type guard
static boolean isNotNull(Date date) {
return date != null;
} Prevention
- Always null-check the Date argument before calling isBefore().
- Use a null-safe comparison utility that returns false for null.
- Ensure date sources (DB, API, parsing) produce non-null values.
When it happens
Trigger: Calling dateTime.isBefore(null). Passing a Date variable from a nullable source (optional DB column, absent JSON field) without null-checking.
Common situations: Comparing against dates from optional fields that may be null. Processing collections of dates where some elements are null. Chaining calls where a prior parse or lookup returned null.
Related errors
- The date must not be null
- Date and Patterns must not be null
- ERA is not support offset!
- This is not a mutable object !
- Parse [{}] with format [{}] error!
AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14).
Data as JSON: /api/errors/f8baf23bf2707e0f.
Report an issue: GitHub.