eclipse-vertx/vert.x · error · NullPointerException

cookie cannot be null

Error message

cookie cannot be null

What it means

CookieJar.add requires a non-null ServerCookie; passing null throws a NullPointerException with the message 'cookie cannot be null'. The jar stores cookies in a sorted list keyed by name/domain/path, so it cannot accept null entries.

Source

Thrown at vertx-core/src/main/java/io/vertx/core/http/impl/CookieJar.java:98

  public Iterator<ServerCookie> iterator() {
    return list.iterator();
  }

  /**
   * Adds a non {@code null} cookie to the cookie jar. Adding cookies is only allowed if the cookie jar is not a slice
   * view of the original cookie jar. In other words if this object was acquired from {@link #getAll(String)} or
   * {@link #removeOrInvalidateAll(String, boolean)} adding cookies will not be allowed.
   *
   * @throws UnsupportedOperationException if cookie jar is a slice view of the http exchange cookies
   * @throws NullPointerException if cookie is {@code null}
   *
   * @param cookie the cookie to add.
   * @return {@code true} if cookie was added or replaced.
   */
  @Override
  public boolean add(ServerCookie cookie) {
    if (cookie == null) {
      throw new NullPointerException("cookie cannot be null");
    }

    for (int i = 0; i < list.size(); i++) {
      int cmp = cookieUniqueIdComparator(list.get(i), cookie.getName(), cookie.getDomain(), cookie.getPath());

      if (cmp > 0) {
        // insert
        list.add(i, cookie);
        return true;
      }
      if (cmp == 0) {
        // replace
        list.set(i, cookie);
        return true;
      }
    }
    // reached the end
    list.add(cookie);

View on GitHub (pinned to fb308bd8c3)

Solutions

  1. Check the cookie for null before adding it and skip/branch when null.
  2. Ensure the cookie is created via ServerCookie before the add call (e.g. cookie from response cookies).
  3. If the cookie is derived from parsing, validate the parse result before inserting into the jar.

Example fix

// before
ServerCookie cookie = parseCookie(headerValue);
jar.add(cookie);
// after
ServerCookie cookie = parseCookie(headerValue);
if (cookie != null) {
  jar.add(cookie);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (cookie == null) return false; // or skip
jar.add(cookie);

Type guard

boolean addIfPresent(CookieJar jar, ServerCookie cookie) {
  return cookie != null && jar.add(cookie);
}

Prevention

When it happens

Trigger: Calling cookieJar.add(null), or a factory/parse method that can return null (e.g. cookie parsing from an absent header value) whose result is added to the jar without a null check.

Common situations: Parsing a Cookie/Set-Cookie header that is missing or empty so the parse helper returns null; conditionally-built cookie variables that end up null; refactored code where the cookie creation was removed but the add call remained.

Related errors


AI-assisted analysis of eclipse-vertx/vert.x@fb308bd8c3 (2026-09-06). Data as JSON: /api/errors/473ae1ee36f6aa32. Report an issue: GitHub.