{"record":{"id":"26b0ed0e76585196","repo":"TooTallNate/Java-WebSocket","slug":"cannot-serialize-a-null-array","errorCode":null,"errorMessage":"Cannot serialize a null array.","messagePattern":"Cannot serialize a null array\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/util/Base64.java","lineNumber":652,"sourceCode":"   * large data sets to encode.\n   *\n   * @param source  The data to convert\n   * @param off     Offset in array where conversion should begin\n   * @param len     Length of data to convert\n   * @param options Specified options\n   * @return The Base64-encoded data as a String\n   * @throws java.io.IOException      if there is an error\n   * @throws IllegalArgumentException if source array is null, if source array, offset, or length\n   *                                  are invalid\n   * @see Base64#GZIP\n   * @see Base64#DO_BREAK_LINES\n   * @since 2.3.1\n   */\n  public static byte[] encodeBytesToBytes(byte[] source, int off, int len, int options)\n      throws java.io.IOException {\n\n    if (source == null) {\n      throw new IllegalArgumentException(\"Cannot serialize a null array.\");\n    }   // end if: null\n\n    if (off < 0) {\n      throw new IllegalArgumentException(\"Cannot have negative offset: \" + off);\n    }   // end if: off < 0\n\n    if (len < 0) {\n      throw new IllegalArgumentException(\"Cannot have length offset: \" + len);\n    }   // end if: len < 0\n\n    if (off + len > source.length) {\n      throw new IllegalArgumentException(\n          String\n              .format(\"Cannot have offset of %d and length of %d with array of length %d\", off, len,\n                  source.length));\n    }   // end if: off < 0\n\n    // Compress?","sourceCodeStart":634,"sourceCodeEnd":670,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/util/Base64.java#L634-L670","documentation":"Base64.encodeBytesToBytes (internal copy of the Robert Harder Base64 utility) rejects a null source array with IllegalArgumentException(\"Cannot serialize a null array.\"). Encoding a null byte array is treated as a caller bug; the library expects a real array (possibly zero-length).","triggerScenarios":"Calling Base64.encodeBytesToBytes(null, off, len, options) directly, or indirectly via encodeBytesToBytes(source) helper with a null array — e.g. encoding an encrypted/serialized payload that came back null from a cipher or serialization step.","commonSituations":"Encoding credentials or binary tokens where an upstream transform returned null (failed encryption, missing resource); Java default-null semantics on optional fields passed straight into the encoder.","solutions":["Null-check the source before encoding and handle the null case explicitly (skip, default to new byte[0], or raise a domain error).","Fix the upstream producer so it returns an empty array instead of null on no-data.","If you only need a String result, use Base64.encodeBytes(source) with the same null guard."],"exampleFix":"// before\nbyte[] b64 = Base64.encodeBytesToBytes(payload); // payload may be null\n// after\nbyte[] b64 = payload == null ? new byte[0] : Base64.encodeBytesToBytes(payload);","handlingStrategy":"type-guard","validationCode":"if (source == null) {\n  throw new IllegalArgumentException(\"Cannot encode a null byte array\");\n}","typeGuard":"static boolean encodable(byte[] source) { return source != null; }","tryCatchPattern":"try {\n  byte[] out = Base64.encodeBytesToBytes(source);\n} catch (IllegalArgumentException e) {\n  log.warn(\"Base64 encode failed: {}\", e.getMessage());\n  out = new byte[0];\n}","preventionTips":["Treat byte[] fields as non-null by convention; default to new byte[0]","Check cipher/serializer outputs for null before feeding them to Base64","Use Objects.requireNonNull at API boundaries to fail fast at the source"],"tags":["base64","encoding","null-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"}