{"record":{"id":"b21adafb2b5317e2","repo":"TheAlgorithms/Java","slug":"character-c-u-04x-not-found-in-huffman-dicti","errorCode":null,"errorMessage":"Character '%c' (U+%04X) not found in Huffman dictionary.","messagePattern":"Character '%c' \\(U\\+%04X\\) not found in Huffman dictionary\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/compression/HuffmanCoding.java","lineNumber":186,"sourceCode":"     * @param text The plaintext string to compress.\n     * @return A string of '0's and '1's representing the compressed data.\n     * Returns an empty string if the input is null or empty.\n     * @throws IllegalStateException    If attempting to encode when the Huffman tree is empty.\n     * @throws IllegalArgumentException If the input text contains a character not present\n     * in the original text used to build the tree.\n     */\n    public String encode(String text) {\n        if (text == null || text.isEmpty()) {\n            return \"\";\n        }\n        if (root == null) {\n            throw new IllegalStateException(\"Huffman tree is empty.\");\n        }\n\n        StringBuilder sb = new StringBuilder();\n        for (char c : text.toCharArray()) {\n            if (!huffmanCodes.containsKey(c)) {\n                throw new IllegalArgumentException(String.format(\"Character '%c' (U+%04X) not found in Huffman dictionary.\", c, (int) c));\n            }\n            sb.append(huffmanCodes.get(c));\n        }\n        return sb.toString();\n    }\n\n    /**\n     * Decodes the given binary string back into the original plaintext using the Huffman Tree.\n     * Validates the integrity of the binary payload during traversal.\n     *\n     * @param encodedText The binary string of '0's and '1's to decompress.\n     * @return The reconstructed plaintext string. Returns an empty string if the input is null or empty.\n     * @throws IllegalStateException    If attempting to decode when the Huffman tree is empty.\n     * @throws IllegalArgumentException If the binary string contains characters other than '0' or '1',\n     * or if the sequence ends abruptly without reaching a leaf node.\n     */\n    public String decode(String encodedText) {\n        if (encodedText == null || encodedText.isEmpty()) {","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/compression/HuffmanCoding.java#L168-L204","documentation":"HuffmanCoding.encode iterates each character of the input and looks it up in the huffmanCodes map built from the training corpus. If a character was never seen when the tree was constructed, it has no code and cannot be encoded — the library rejects it rather than silently dropping or mis-encoding the character.","triggerScenarios":"Building the tree from \"aabb\" then calling encode(\"aabc\") (c is unknown); encoding text whose alphabet differs from the training corpus; encoding after the corpus was a strict subset.","commonSituations":"Training corpus did not cover all characters that appear in production data; a new/edge-case character appears at runtime; case mismatch (tree built on lowercase, encode given uppercase).","solutions":["Rebuild the Huffman tree from a corpus that includes every character you intend to encode.","Validate that every character in the input is present in getHuffmanCodes() before encoding.","Normalize input (e.g. casing) to match the corpus alphabet."],"exampleFix":"// before\nHuffmanCoding hc = new HuffmanCoding(\"aabb\");\nString enc = hc.encode(\"aabc\"); // c not in tree\n\n// after\nHuffmanCoding hc = new HuffmanCoding(corpus); // corpus includes c\nfor (char ch : message.toCharArray()) {\n    if (!hc.getHuffmanCodes().containsKey(ch)) {\n        throw new IllegalArgumentException(\"Character \" + ch + \" absent from Huffman dictionary\");\n    }\n}\nString enc = hc.encode(message);","handlingStrategy":"validation","validationCode":"Map<Character, String> codes = hc.getHuffmanCodes();\nfor (char c : text.toCharArray()) {\n    if (!codes.containsKey(c)) {\n        throw new IllegalArgumentException(\"Character \" + c + \" not in Huffman dictionary; rebuild tree from a fuller corpus\");\n    }\n}\nString enc = hc.encode(text);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Build the tree from a corpus covering every character you will encode.","Validate input characters against getHuffmanCodes() before encoding.","Normalize casing/characters to match the corpus alphabet."],"tags":["compression","huffman","dictionary","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}