TheAlgorithms/Java · error · IllegalArgumentException
Character array and frequency array cannot be empty
Error message
Character array and frequency array cannot be empty
What it means
Thrown by Huffman.buildHuffmanTree when charArray.length == 0 or charFreq.length == 0. A Huffman tree needs at least one symbol to build; an empty alphabet has no leaves. The check runs after the null check and before the length-match check.
Source
Thrown at src/main/java/com/thealgorithms/others/Huffman.java:85
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);
}
// Build the Huffman treeView on GitHub (pinned to fdfb9a395b)
Solutions
- Verify the symbol/frequency source yields at least one entry before calling.
- Branch around the call if an empty alphabet is legitimate in your domain.
- Log the array lengths to confirm emptiness at the call site.
Example fix
// before
char[] syms = symbols.toArray(new char[0]);
int[] frq = freqs.stream().mapToInt(Integer::intValue).toArray();
HuffmanNode root = Huffman.buildHuffmanTree(syms, frq); // throws if input empty
// after
if (symbols.isEmpty()) throw new IllegalStateException("No symbols to encode");
char[] syms = ...; int[] frq = ...;
HuffmanNode root = Huffman.buildHuffmanTree(syms, frq); Defensive patterns
Strategy: validation
Validate before calling
if (charArray.length == 0 || charFreq.length == 0) {
throw new IllegalStateException("Cannot build Huffman tree from empty alphabet");
} Prevention
- Branch around buildHuffmanTree when the alphabet may be empty.
- Verify the symbol source yields at least one entry.
When it happens
Trigger: Calling buildHuffmanTree with empty arrays — e.g., building a frequency table from an empty input string, or converting an empty map to arrays.
Common situations: Empty input text producing no symbols, a filter that removed all symbols, or a default-empty frequency table.
Related errors
- Character array and frequency array cannot be null
- Character array and frequency array must have the same lengt
- Frequencies must be non-negative
- Huffman tree is empty.
- Character '%c' (U+%04X) not found in Huffman dictionary.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/ac8347cb849023a0.
Report an issue: GitHub.