{"record":{"id":"52ffaa6661755e23","repo":"TooTallNate/Java-WebSocket","slug":"cannot-send-null-data-to-a-websocketimpl","errorCode":null,"errorMessage":"Cannot send 'null' data to a WebSocketImpl.","messagePattern":"Cannot send 'null' data to a WebSocketImpl\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/WebSocketImpl.java","lineNumber":643,"sourceCode":"\n  @Override\n  public void close(int code) {\n    close(code, \"\", false);\n  }\n\n  public void close(InvalidDataException e) {\n    close(e.getCloseCode(), e.getMessage(), false);\n  }\n\n  /**\n   * Send Text data to the other end.\n   *\n   * @throws WebsocketNotConnectedException websocket is not yet connected\n   */\n  @Override\n  public void send(String text) {\n    if (text == null) {\n      throw new IllegalArgumentException(\"Cannot send 'null' data to a WebSocketImpl.\");\n    }\n    send(draft.createFrames(text, role == Role.CLIENT));\n  }\n\n  /**\n   * Send Binary data (plain bytes) to the other end.\n   *\n   * @throws IllegalArgumentException       the data is null\n   * @throws WebsocketNotConnectedException websocket is not yet connected\n   */\n  @Override\n  public void send(ByteBuffer bytes) {\n    if (bytes == null) {\n      throw new IllegalArgumentException(\"Cannot send 'null' data to a WebSocketImpl.\");\n    }\n    send(draft.createFrames(bytes, role == Role.CLIENT));\n  }\n","sourceCodeStart":625,"sourceCodeEnd":661,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/WebSocketImpl.java#L625-L661","documentation":"send(String) throws IllegalArgumentException when the text argument is null. The library refuses to build frames from null payloads because a text message must have content; this is a fail-fast guard before draft.createFrames() would fail.","triggerScenarios":"Calling websocket.send((String) null), e.g. when the payload comes from a method that returned null or an uninitialized variable.","commonSituations":"Passing results of JSON lookups or database reads that can be null; generic send helpers that don't null-check their parameters; overloaded send(byte[]) vs send(String) confusion producing a null cast.","solutions":["Null-check the payload before calling send().","Send an empty string (\"\") explicitly if an empty message is intended.","Catch IllegalArgumentException around send if null payloads are possible from upstream.","Fix the upstream source so it never produces a null message."],"exampleFix":"// before\nws.send(maybeNullText);\n// after\nif (maybeNullText != null) {\n  ws.send(maybeNullText);\n}","handlingStrategy":"validation","validationCode":"if (text == null) { text = \"\"; } // or skip the send","typeGuard":"if (text instanceof String) { conn.send(text); }","tryCatchPattern":"try { conn.send(text); } catch (IllegalArgumentException e) { log.warn(\"null text payload dropped\"); }","preventionTips":["Null-check payloads from JSON/DB lookups before send","Use explicit empty strings instead of null","Wrap generic send helpers with null guards"],"tags":["null-check","send","illegal-argument"],"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"}