TooTallNate/Java-WebSocket · error · IllegalArgumentException

Cannot serialize a null array.

Error message

Cannot serialize a null array.

What it means

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).

Solutions

  1. Null-check the source before encoding and handle the null case explicitly (skip, default to new byte[0], or raise a domain error).
  2. Fix the upstream producer so it returns an empty array instead of null on no-data.
  3. If you only need a String result, use Base64.encodeBytes(source) with the same null guard.

Example fix

// before
byte[] b64 = Base64.encodeBytesToBytes(payload); // payload may be null
// after
byte[] b64 = payload == null ? new byte[0] : Base64.encodeBytesToBytes(payload);
Defensive patterns

Strategy: type-guard

Validate before calling

if (source == null) {
  throw new IllegalArgumentException("Cannot encode a null byte array");
}

Type guard

static boolean encodable(byte[] source) { return source != null; }

Try / catch

try {
  byte[] out = Base64.encodeBytesToBytes(source);
} catch (IllegalArgumentException e) {
  log.warn("Base64 encode failed: {}", e.getMessage());
  out = new byte[0];
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TooTallNate/Java-WebSocket@afeacbf8c0 (2026-09-09). Data as JSON: /api/errors/26b0ed0e76585196. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/org/java_websocket/util/Base64.java:652

   * large data sets to encode.
   *
   * @param source  The data to convert
   * @param off     Offset in array where conversion should begin
   * @param len     Length of data to convert
   * @param options Specified options
   * @return The Base64-encoded data as a String
   * @throws java.io.IOException      if there is an error
   * @throws IllegalArgumentException if source array is null, if source array, offset, or length
   *                                  are invalid
   * @see Base64#GZIP
   * @see Base64#DO_BREAK_LINES
   * @since 2.3.1
   */
  public static byte[] encodeBytesToBytes(byte[] source, int off, int len, int options)
      throws java.io.IOException {

    if (source == null) {
      throw new IllegalArgumentException("Cannot serialize a null array.");
    }   // end if: null

    if (off < 0) {
      throw new IllegalArgumentException("Cannot have negative offset: " + off);
    }   // end if: off < 0

    if (len < 0) {
      throw new IllegalArgumentException("Cannot have length offset: " + len);
    }   // end if: len < 0

    if (off + len > source.length) {
      throw new IllegalArgumentException(
          String
              .format("Cannot have offset of %d and length of %d with array of length %d", off, len,
                  source.length));
    }   // end if: off < 0

    // Compress?

View on GitHub (pinned to afeacbf8c0)