TheAlgorithms/Java · error · IllegalArgumentException

Character array and frequency array cannot be null

Error message

Character array and frequency array cannot be null

What it means

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).

Source

Thrown at src/main/java/com/thealgorithms/others/Huffman.java:82

 * @see <a href="https://en.wikipedia.org/wiki/Huffman_coding">Huffman
 *      Coding</a>
 */
public final class Huffman {
    private Huffman() {
    }

    /**
     * Builds a Huffman tree from the given character array and their frequencies.
     *
     * @param charArray array of characters
     * @param charFreq  array of frequencies corresponding to the characters
     * @return root node of the Huffman tree
     * @throws IllegalArgumentException if arrays are null, empty, or have different
     *                                  lengths
     */
    public static HuffmanNode buildHuffmanTree(char[] charArray, int[] charFreq) {
        if (charArray == null || charFreq == null) {
            throw new IllegalArgumentException("Character array and frequency array cannot be null");
        }
        if (charArray.length == 0 || charFreq.length == 0) {
            throw new IllegalArgumentException("Character array and frequency array cannot be empty");
        }
        if (charArray.length != charFreq.length) {
            throw new IllegalArgumentException("Character array and frequency array must have the same length");
        }

        int n = charArray.length;
        PriorityQueue<HuffmanNode> priorityQueue = new PriorityQueue<>(n, new HuffmanComparator());

        // Create leaf nodes and add to priority queue
        for (int i = 0; i < n; i++) {
            if (charFreq[i] < 0) {
                throw new IllegalArgumentException("Frequencies must be non-negative");
            }
            HuffmanNode node = new HuffmanNode(charArray[i], charFreq[i]);
            priorityQueue.add(node);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure both arrays are non-null and populated before the call.
  2. Build the frequency table with defaults so no null is ever returned.
  3. Add an explicit null guard at the call site with context.

Example fix

// before
char[] syms = symbolsFrom(input); // may be null
int[] frq = freqsFrom(input);
HuffmanNode root = Huffman.buildHuffmanTree(syms, frq);

// after
char[] syms = symbolsFrom(input);
int[] frq = freqsFrom(input);
if (syms == null || frq == null) throw new IllegalStateException("Missing symbols/frequencies");
HuffmanNode root = Huffman.buildHuffmanTree(syms, frq);
Defensive patterns

Strategy: validation

Validate before calling

Objects.requireNonNull(charArray, "charArray");
Objects.requireNonNull(charFreq, "charFreq");
Huffman.buildHuffmanTree(charArray, charFreq);

Prevention

When it happens

Trigger: Calling buildHuffmanTree(null, freq), buildHuffmanTree(chars, null), or both null — typically when one array came from a map/lookup that returned null.

Common situations: 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.

Related errors


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