{"record":{"id":"528506303d1ddf2c","repo":"apache/hadoop","slug":"token-cannot-be-null","errorCode":null,"errorMessage":"token cannot be NULL","messagePattern":"token cannot be NULL","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-auth/src/main/java/org/apache/hadoop/security/authentication/client/AuthenticatedURL.java","lineNumber":351,"sourceCode":"   * Returns an authenticated {@link HttpURLConnection}.\n   *\n   * @param url the URL to connect to. Only HTTP/S URLs are supported.\n   * @param token the authentication token being used for the user.\n   *\n   * @return an authenticated {@link HttpURLConnection}.\n   *\n   * @throws IOException if an IO error occurred.\n   * @throws AuthenticationException if an authentication exception occurred.\n   */\n  public HttpURLConnection openConnection(URL url, Token token) throws IOException, AuthenticationException {\n    if (url == null) {\n      throw new IllegalArgumentException(\"url cannot be NULL\");\n    }\n    if (!url.getProtocol().equalsIgnoreCase(\"http\") && !url.getProtocol().equalsIgnoreCase(\"https\")) {\n      throw new IllegalArgumentException(\"url must be for a HTTP or HTTPS resource\");\n    }\n    if (token == null) {\n      throw new IllegalArgumentException(\"token cannot be NULL\");\n    }\n    authenticator.authenticate(url, token);\n\n    // allow the token to create the connection with a cookie handler for\n    // managing session cookies.\n    return token.openConnection(url, connConfigurator);\n  }\n\n  /**\n   * Helper method that injects an authentication token to send with a\n   * connection. Callers should prefer using\n   * {@link Token#openConnection(URL, ConnectionConfigurator)} which\n   * automatically manages authentication tokens.\n   *\n   * @param conn connection to inject the authentication token into.\n   * @param token authentication token to inject.\n   */\n  public static void injectToken(HttpURLConnection conn, Token token) {","sourceCodeStart":333,"sourceCodeEnd":369,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-auth/src/main/java/org/apache/hadoop/security/authentication/client/AuthenticatedURL.java#L333-L369","documentation":"The third precondition of AuthenticatedURL.openConnection: the Token argument must be non-null. The token is both input (carries an existing authenticated cookie, if any) and output (receives the cookie the authenticator obtains); a null token makes that handshake impossible, so the method rejects it before contacting the server. Callers normally pass a Token created with the no-arg constructor.","triggerScenarios":"Passing a null Token because the field was never initialized, a helper method with an optional token parameter defaulting to null, or code that skips token creation when it 'just wants a connection' without authentication state.","commonSituations":"Refactoring shared HTTP client utilities where the token was made optional; copy-pasted examples that omit the Token line; test code constructing AuthenticatedURL but forgetting the token argument ordering.","solutions":["Always create the holder: AuthenticatedURL.Token token = new AuthenticatedURL.Token(); then pass it to openConnection — it will be populated during authentication.","Reuse one Token across sequential requests to keep the session cookie (authenticated connections reuse it).","If a helper accepts an optional token, substitute new Token() for null inside the helper.","Check for null tokens from upstream factories (e.g. credentials cache) before calling."],"exampleFix":"// before\nnew AuthenticatedURL().openConnection(url, null);\n\n// after\nAuthenticatedURL.Token token = new AuthenticatedURL.Token();\nHttpURLConnection conn = new AuthenticatedURL().openConnection(url, token);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(token, \"Token must be created (new AuthenticatedURL.Token()) before openConnection\");\nnew AuthenticatedURL().openConnection(url, token);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always allocate Token with the no-arg constructor even when starting unauthenticated.","Reuse one Token across requests to preserve the session cookie.","Make optional-token helper parameters default to a fresh Token, not null."],"tags":["authentication","null-argument","token","hadoop-auth","precondition"],"backgroundTag":"null-argument","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}