{"record":{"id":"8079c20d0dacd70c","repo":"TooTallNate/Java-WebSocket","slug":"parameter-must-not-be-null-8079c2","errorCode":null,"errorMessage":"parameter must not be null","messagePattern":"parameter must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/SSLSocketChannel2.java","lineNumber":111,"sourceCode":"   * used to set interestOP SelectionKey.OP_WRITE for the underlying channel\n   */\n  protected SelectionKey selectionKey;\n\n  protected SSLEngine sslEngine;\n  protected SSLEngineResult readEngineResult;\n  protected SSLEngineResult writeEngineResult;\n\n  /**\n   * Should be used to count the buffer allocations. But because of #190 where\n   * HandshakeStatus.FINISHED is not properly returned by nio wrap/unwrap this variable is used to\n   * check whether {@link #createBuffers(SSLSession)} needs to be called.\n   **/\n  protected int bufferallocations = 0;\n\n  public SSLSocketChannel2(SocketChannel channel, SSLEngine sslEngine, ExecutorService exec,\n      SelectionKey key) throws IOException {\n    if (channel == null || sslEngine == null || exec == null) {\n      throw new IllegalArgumentException(\"parameter must not be null\");\n    }\n\n    this.socketChannel = channel;\n    this.sslEngine = sslEngine;\n    this.exec = exec;\n\n    readEngineResult = writeEngineResult = new SSLEngineResult(Status.BUFFER_UNDERFLOW,\n        sslEngine.getHandshakeStatus(), 0, 0); // init to prevent NPEs\n\n    tasks = new ArrayList<Future<?>>(3);\n    if (key != null) {\n      key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);\n      this.selectionKey = key;\n    }\n    createBuffers(sslEngine.getSession());\n    // kick off handshake\n    socketChannel.write(wrap(emptybuffer));// initializes res\n    processHandshake(false);","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/SSLSocketChannel2.java#L93-L129","documentation":"The SSLSocketChannel2 constructor validates that the SocketChannel, SSLEngine and ExecutorService are all non-null and throws IllegalArgumentException('parameter must not be null') otherwise. It exists so the SSL wrapper fails fast instead of producing a NullPointerException deep inside the I/O loop.","triggerScenarios":"Calling new SSLSocketChannel2(channel, sslEngine, exec, key) with any of channel, sslEngine or exec null — e.g. SSLContext initialization failed silently, the accept returned a null channel, or the server's executor was not initialized.","commonSituations":"Misconfigured keystore/truststore leading to a null SSLEngine; forgetting to set the worker ExecutorService on a custom WebSocketServer; wiring the channel manually instead of via WebSocketServer factory methods.","solutions":["Create the SSLEngine via a properly configured SSLContext before constructing the channel","Pass a running ExecutorService (e.g. Executors.newCachedThreadPool()) — never null","Add null checks/guards before calling the constructor and fail with a descriptive message"],"exampleFix":"// before\nnew SSLSocketChannel2(channel, null, exec, key); // NPE-prone engine setup\n// after\nSSLContext ctx = buildSslContext(keystore, password); // throws early if config is bad\nSSLEngine engine = ctx.createSSLEngine();\nengine.setUseClientMode(false);\nnew SSLSocketChannel2(channel, engine, Executors.newCachedThreadPool(), key);","handlingStrategy":"validation","validationCode":"if (channel == null || sslEngine == null || exec == null) {\n  throw new IllegalArgumentException(\"SSLSocketChannel2 requires channel, sslEngine and exec\");\n}","typeGuard":"boolean ready = channel != null && sslEngine != null && exec != null;\nif (!ready) { failFast(\"SSL channel dependencies missing\"); }","tryCatchPattern":"try { new SSLSocketChannel2(ch, engine, exec, key); } catch (IllegalArgumentException e) { log.error(\"SSL channel init failed: {}\", e.getMessage()); throw e; }","preventionTips":["Initialize SSLContext/SSLEngine and the worker ExecutorService before server startup","Validate constructor arguments in your own wiring layer for clearer error messages","Ensure keystore/truststore configuration is valid so sslEngine is never null"],"tags":["websocket","ssl","null-check","constructor"],"backgroundTag":"null-argument","analyzedSha":"afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d","analyzedAt":"2026-09-09T14:39:47.546Z","contentChangedAt":"2026-09-09T14:39:47.546Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}