{"record":{"id":"473ae1ee36f6aa32","repo":"eclipse-vertx/vert.x","slug":"cookie-cannot-be-null","errorCode":null,"errorMessage":"cookie cannot be null","messagePattern":"cookie cannot be null","errorType":"validation","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"vertx-core/src/main/java/io/vertx/core/http/impl/CookieJar.java","lineNumber":98,"sourceCode":"  public Iterator<ServerCookie> iterator() {\n    return list.iterator();\n  }\n\n  /**\n   * Adds a non {@code null} cookie to the cookie jar. Adding cookies is only allowed if the cookie jar is not a slice\n   * view of the original cookie jar. In other words if this object was acquired from {@link #getAll(String)} or\n   * {@link #removeOrInvalidateAll(String, boolean)} adding cookies will not be allowed.\n   *\n   * @throws UnsupportedOperationException if cookie jar is a slice view of the http exchange cookies\n   * @throws NullPointerException if cookie is {@code null}\n   *\n   * @param cookie the cookie to add.\n   * @return {@code true} if cookie was added or replaced.\n   */\n  @Override\n  public boolean add(ServerCookie cookie) {\n    if (cookie == null) {\n      throw new NullPointerException(\"cookie cannot be null\");\n    }\n\n    for (int i = 0; i < list.size(); i++) {\n      int cmp = cookieUniqueIdComparator(list.get(i), cookie.getName(), cookie.getDomain(), cookie.getPath());\n\n      if (cmp > 0) {\n        // insert\n        list.add(i, cookie);\n        return true;\n      }\n      if (cmp == 0) {\n        // replace\n        list.set(i, cookie);\n        return true;\n      }\n    }\n    // reached the end\n    list.add(cookie);","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/eclipse-vertx/vert.x/blob/fb308bd8c3f12c79f4ae89bef67fadf6c80d036e/vertx-core/src/main/java/io/vertx/core/http/impl/CookieJar.java#L80-L116","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the cookie for null before adding it and skip/branch when null.","Ensure the cookie is created via ServerCookie before the add call (e.g. cookie from response cookies).","If the cookie is derived from parsing, validate the parse result before inserting into the jar."],"exampleFix":"// before\nServerCookie cookie = parseCookie(headerValue);\njar.add(cookie);\n// after\nServerCookie cookie = parseCookie(headerValue);\nif (cookie != null) {\n  jar.add(cookie);\n}","handlingStrategy":"type-guard","validationCode":"if (cookie == null) return false; // or skip\njar.add(cookie);","typeGuard":"boolean addIfPresent(CookieJar jar, ServerCookie cookie) {\n  return cookie != null && jar.add(cookie);\n}","tryCatchPattern":null,"preventionTips":["Null-check parse results of Set-Cookie headers before adding.","Avoid factory methods that can return null for cookies; prefer Optional.","Document jar.add as not-null-accepting in wrapper utilities."],"tags":["cookie","null-check","http"],"backgroundTag":"null-argument","analyzedSha":"fb308bd8c3f12c79f4ae89bef67fadf6c80d036e","analyzedAt":"2026-09-06T11:37:12.241Z","contentChangedAt":"2026-09-06T11:37:12.241Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}