{"record":{"id":"6d07de013ea096eb","repo":"TooTallNate/Java-WebSocket","slug":"destination-array-was-null","errorCode":null,"errorMessage":"Destination array was null.","messagePattern":"Destination array was null\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/java_websocket/util/Base64.java","lineNumber":811,"sourceCode":"   * @param destination the array to hold the conversion\n   * @param destOffset  the index where output will be put\n   * @param options     alphabet type is pulled from this (standard, url-safe, ordered)\n   * @return the number of decoded bytes converted\n   * @throws IllegalArgumentException if source or destination arrays are null, if srcOffset or\n   *                                  destOffset are invalid or there is not enough room in the\n   *                                  array.\n   * @since 1.3\n   */\n  private static int decode4to3(\n      byte[] source, int srcOffset,\n      byte[] destination, int destOffset, int options) {\n\n    // Lots of error checking and exception throwing\n    if (source == null) {\n      throw new IllegalArgumentException(\"Source array was null.\");\n    }   // end if\n    if (destination == null) {\n      throw new IllegalArgumentException(\"Destination array was null.\");\n    }   // end if\n    if (srcOffset < 0 || srcOffset + 3 >= source.length) {\n      throw new IllegalArgumentException(String.format(\n          \"Source array with length %d cannot have offset of %d and still process four bytes.\",\n          source.length, srcOffset));\n    }   // end if\n    if (destOffset < 0 || destOffset + 2 >= destination.length) {\n      throw new IllegalArgumentException(String.format(\n          \"Destination array with length %d cannot have offset of %d and still store three bytes.\",\n          destination.length, destOffset));\n    }   // end if\n\n    final byte[] DECODABET = getDecodabet(options);\n\n    // Example: Dk==\n    if (source[srcOffset + 2] == EQUALS_SIGN) {\n      // Two ways to do the same thing. Don't know which way I like best.\n      //int outBuff =   ( ( DECODABET[ source[ srcOffset    ] ] << 24 ) >>>  6 )","sourceCodeStart":793,"sourceCodeEnd":829,"githubUrl":"https://github.com/TooTallNate/Java-WebSocket/blob/afeacbf8c0f6f6a761c9d9daed8c813dd3b8ed7d/src/main/java/org/java_websocket/util/Base64.java#L793-L829","documentation":"decode4to3 also requires a non-null destination array to write the 3 decoded bytes into. A null destination throws IllegalArgumentException immediately. This normally indicates a null output buffer passed down from a public decode method.","triggerScenarios":"A decode variant that takes an explicit destination byte[] (e.g. decode(source, offset, length, options) style overloads or decodeBytes with output buffer) is given a null destination.","commonSituations":"Reusing a buffer variable that was never initialized; a lazily-allocated output buffer that was still null when decoding was attempted; API misuse where the user confused source and destination parameters.","solutions":["Ensure the destination array is allocated (new byte[...]) before calling the decode API.","Check argument order — source and destination were likely swapped.","Add a null assert/guard before invoking decode if the buffer comes from a pool or lazy allocation."],"exampleFix":"// before\nbyte[] dest = null;\nBase64.decodeBytes(src, 0, src.length, dest, 0);\n\n// after\nbyte[] dest = new byte[Base64.decodeBytesToBytes(src, 0, src.length).length];\nBase64.decode(src, 0, src.length, dest, 0);","handlingStrategy":"type-guard","validationCode":"if (src != null && dest != null) {\n    Base64.decode(src, 0, src.length, dest, 0);\n}","typeGuard":"static boolean hasBuffers(byte[] src, byte[] dest) {\n    return src != null && dest != null;\n}","tryCatchPattern":"try {\n    Base64.decode(src, 0, src.length, dest, 0);\n} catch (IllegalArgumentException e) {\n    // destination was null or too small\n    throw new IllegalStateException(\"decode output buffer not initialized\", e);\n}","preventionTips":["Allocate output buffers eagerly, not lazily, on decode paths.","Double-check parameter order for (source, dest) overloads — swapping them is the most common cause.","Use the single-shot decode APIs that allocate the destination internally whenever possible."],"tags":["java","base64","null","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"}