{"record":{"id":"1d3430eebcd36f17","repo":"TheAlgorithms/Java","slug":"input-cannot-be-null","errorCode":null,"errorMessage":"Input cannot be null","messagePattern":"Input cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/Base64.java","lineNumber":39,"sourceCode":"public final class Base64 {\n\n    // Base64 character set\n    private static final String BASE64_CHARS = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\";\n    private static final char PADDING_CHAR = '=';\n\n    private Base64() {\n    }\n\n    /**\n     * Encodes the given byte array to a Base64 encoded string.\n     *\n     * @param input the byte array to encode\n     * @return the Base64 encoded string\n     * @throws IllegalArgumentException if input is null\n     */\n    public static String encode(byte[] input) {\n        if (input == null) {\n            throw new IllegalArgumentException(\"Input cannot be null\");\n        }\n\n        if (input.length == 0) {\n            return \"\";\n        }\n\n        StringBuilder result = new StringBuilder();\n        int padding = 0;\n\n        // Process input in groups of 3 bytes\n        for (int i = 0; i < input.length; i += 3) {\n            // Get up to 3 bytes\n            int byte1 = input[i] & 0xFF;\n            int byte2 = (i + 1 < input.length) ? (input[i + 1] & 0xFF) : 0;\n            int byte3 = (i + 2 < input.length) ? (input[i + 2] & 0xFF) : 0;\n\n            // Calculate padding needed\n            if (i + 1 >= input.length) {","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/Base64.java#L21-L57","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Null-check the source before calling encode, and either skip encoding or substitute an empty byte array.","Fix the upstream producer so it never returns null (return byte[0] or throw a domain-specific exception).","Use Objects.requireNonNull(input, \"input\") earlier to fail at the boundary with a clearer context."],"exampleFix":"// before\nbyte[] data = readFile(path); // returns null if missing\nString b64 = Base64.encode(data);\n\n// after\nbyte[] data = readFile(path);\nString b64 = Base64.encode(data == null ? new byte[0] : data);","handlingStrategy":"validation","validationCode":"if (input == null) {\n    input = new byte[0]; // or throw a domain exception\n}\nString b64 = Base64.encode(input);","typeGuard":"static boolean isNonNullBytes(byte[] b) {\n    return b != null;\n}","tryCatchPattern":"try {\n    String b64 = Base64.encode(data);\n} catch (IllegalArgumentException e) {\n    throw new DomainException(\"Cannot encode null input\", e);\n}","preventionTips":["Null-check at the source (file read, network) and substitute byte[0].","Use Objects.requireNonNull(data, \"data\") at your own boundary.","Avoid returning null from producers; prefer empty arrays."],"tags":["conversions","base64","null-check","validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}