TheAlgorithms/Java · error · IllegalArgumentException

Input cannot be null

Error message

Input cannot be null

What it means

Base64.encode(byte[]) rejects a null input array. The method performs no implicit defaulting; a null reference would otherwise cause an NPE at input.length, so the guard surfaces the problem explicitly with a clear message.

Source

Thrown at src/main/java/com/thealgorithms/conversions/Base64.java:39

public final class Base64 {

    // Base64 character set
    private static final String BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    private static final char PADDING_CHAR = '=';

    private Base64() {
    }

    /**
     * Encodes the given byte array to a Base64 encoded string.
     *
     * @param input the byte array to encode
     * @return the Base64 encoded string
     * @throws IllegalArgumentException if input is null
     */
    public static String encode(byte[] input) {
        if (input == null) {
            throw new IllegalArgumentException("Input cannot be null");
        }

        if (input.length == 0) {
            return "";
        }

        StringBuilder result = new StringBuilder();
        int padding = 0;

        // Process input in groups of 3 bytes
        for (int i = 0; i < input.length; i += 3) {
            // Get up to 3 bytes
            int byte1 = input[i] & 0xFF;
            int byte2 = (i + 1 < input.length) ? (input[i + 1] & 0xFF) : 0;
            int byte3 = (i + 2 < input.length) ? (input[i + 2] & 0xFF) : 0;

            // Calculate padding needed
            if (i + 1 >= input.length) {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Null-check the source before calling encode, and either skip encoding or substitute an empty byte array.
  2. Fix the upstream producer so it never returns null (return byte[0] or throw a domain-specific exception).
  3. Use Objects.requireNonNull(input, "input") earlier to fail at the boundary with a clearer context.

Example fix

// before
byte[] data = readFile(path); // returns null if missing
String b64 = Base64.encode(data);

// after
byte[] data = readFile(path);
String b64 = Base64.encode(data == null ? new byte[0] : data);
Defensive patterns

Strategy: validation

Validate before calling

if (input == null) {
    input = new byte[0]; // or throw a domain exception
}
String b64 = Base64.encode(input);

Type guard

static boolean isNonNullBytes(byte[] b) {
    return b != null;
}

Try / catch

try {
    String b64 = Base64.encode(data);
} catch (IllegalArgumentException e) {
    throw new DomainException("Cannot encode null input", e);
}

Prevention

When it happens

Trigger: Passing a null byte array from an upstream source that failed to produce data (e.g., a missing file read, an empty HTTP response body cast to byte[]). Calling encode with an uninitialized field.

Common situations: Reading a resource that may be absent and forwarding the null result. Deserialization or network code returning null on error. Unit tests omitting array initialization.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/1d3430eebcd36f17. Report an issue: GitHub.