{"record":{"id":"0b2574cb6d24d487","repo":"TheAlgorithms/Java","slug":"character-array-and-frequency-array-cannot-be-null","errorCode":null,"errorMessage":"Character array and frequency array cannot be null","messagePattern":"Character array and frequency array cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/Huffman.java","lineNumber":82,"sourceCode":" * @see <a href=\"https://en.wikipedia.org/wiki/Huffman_coding\">Huffman\n *      Coding</a>\n */\npublic final class Huffman {\n    private Huffman() {\n    }\n\n    /**\n     * Builds a Huffman tree from the given character array and their frequencies.\n     *\n     * @param charArray array of characters\n     * @param charFreq  array of frequencies corresponding to the characters\n     * @return root node of the Huffman tree\n     * @throws IllegalArgumentException if arrays are null, empty, or have different\n     *                                  lengths\n     */\n    public static HuffmanNode buildHuffmanTree(char[] charArray, int[] charFreq) {\n        if (charArray == null || charFreq == null) {\n            throw new IllegalArgumentException(\"Character array and frequency array cannot be null\");\n        }\n        if (charArray.length == 0 || charFreq.length == 0) {\n            throw new IllegalArgumentException(\"Character array and frequency array cannot be empty\");\n        }\n        if (charArray.length != charFreq.length) {\n            throw new IllegalArgumentException(\"Character array and frequency array must have the same length\");\n        }\n\n        int n = charArray.length;\n        PriorityQueue<HuffmanNode> priorityQueue = new PriorityQueue<>(n, new HuffmanComparator());\n\n        // Create leaf nodes and add to priority queue\n        for (int i = 0; i < n; i++) {\n            if (charFreq[i] < 0) {\n                throw new IllegalArgumentException(\"Frequencies must be non-negative\");\n            }\n            HuffmanNode node = new HuffmanNode(charArray[i], charFreq[i]);\n            priorityQueue.add(node);","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/Huffman.java#L64-L100","documentation":"Thrown by Huffman.buildHuffmanTree when charArray or charFreq is null. The Huffman algorithm pairs symbols with frequencies into leaf nodes; if either array is null there is nothing to enqueue into the priority queue. This is the first of several precondition checks (null → empty → length-match → non-negative frequency).","triggerScenarios":"Calling buildHuffmanTree(null, freq), buildHuffmanTree(chars, null), or both null — typically when one array came from a map/lookup that returned null.","commonSituations":"A frequency table built from input where a symbol had no entry, a refactor that left a field null, or a deserialized structure missing one array.","solutions":["Ensure both arrays are non-null and populated before the call.","Build the frequency table with defaults so no null is ever returned.","Add an explicit null guard at the call site with context."],"exampleFix":"// before\nchar[] syms = symbolsFrom(input); // may be null\nint[] frq = freqsFrom(input);\nHuffmanNode root = Huffman.buildHuffmanTree(syms, frq);\n\n// after\nchar[] syms = symbolsFrom(input);\nint[] frq = freqsFrom(input);\nif (syms == null || frq == null) throw new IllegalStateException(\"Missing symbols/frequencies\");\nHuffmanNode root = Huffman.buildHuffmanTree(syms, frq);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(charArray, \"charArray\");\nObjects.requireNonNull(charFreq, \"charFreq\");\nHuffman.buildHuffmanTree(charArray, charFreq);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Build the symbol and frequency arrays from one source so neither is null.","Add Objects.requireNonNull at the boundary."],"tags":["huffman","null-check","precondition","compression","tree"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}