TheAlgorithms/Java · error · IllegalArgumentException

Alphabet cannot be null or empty when text is not empty.

Error message

Alphabet cannot be null or empty when text is not empty.

What it means

MoveToFront.transform(String, String) returns an empty list for null/empty text, but when the text is non-empty it requires a non-null, non-empty initialAlphabet because it must look up each character's index within the alphabet. Without an alphabet the lookup is undefined, so the guard fails fast.

Source

Thrown at src/main/java/com/thealgorithms/compression/MoveToFront.java:96

     * all unique characters that may appear in the input.</p>
     *
     * @param text the input string to transform; if empty, returns an empty list
     * @param initialAlphabet a string containing the initial ordered set of symbols
     *                        (e.g., "$abn" or the full ASCII set); must not be empty
     *                        when {@code text} is non-empty
     * @return a list of integers representing the transformed data, where each integer
     *         is the index of the corresponding input character in the current alphabet state
     * @throws IllegalArgumentException if {@code text} is non-empty and {@code initialAlphabet}
     *                                  is {@code null} or empty
     * @throws IllegalArgumentException if any character in {@code text} is not found in
     *                                  {@code initialAlphabet}
     */
    public static List<Integer> transform(String text, String initialAlphabet) {
        if (text == null || text.isEmpty()) {
            return new ArrayList<>();
        }
        if (initialAlphabet == null || initialAlphabet.isEmpty()) {
            throw new IllegalArgumentException("Alphabet cannot be null or empty when text is not empty.");
        }

        List<Integer> output = new ArrayList<>(text.length());

        // Use LinkedList for O(1) add-to-front and O(n) remove operations
        // This is more efficient than ArrayList for the move-to-front pattern
        List<Character> alphabet = initialAlphabet.chars().mapToObj(c -> (char) c).collect(Collectors.toCollection(LinkedList::new));

        for (char c : text.toCharArray()) {
            int index = alphabet.indexOf(c);
            if (index == -1) {
                throw new IllegalArgumentException("Symbol '" + c + "' not found in the initial alphabet.");
            }

            output.add(index);

            // Move the character to the front
            Character symbol = alphabet.remove(index);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass a non-empty alphabet such as "abcdefghijklmnopqrstuvwxyz" that contains every character in the text.
  2. Validate the alphabet is non-null and non-empty before calling transform when the text is non-empty.
  3. Derive the alphabet from the text's distinct characters as a fallback.

Example fix

// before
List<Integer> out = MoveToFront.transform(text, alphabet); // alphabet null

// after
if (alphabet == null || alphabet.isEmpty()) {
    alphabet = text.chars().distinct()
        .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
        .toString();
}
List<Integer> out = MoveToFront.transform(text, alphabet);
Defensive patterns

Strategy: validation

Validate before calling

if (text != null && !text.isEmpty() && (initialAlphabet == null || initialAlphabet.isEmpty())) {
    throw new IllegalArgumentException("Alphabet must be non-empty when text is non-empty");
}
List<Integer> out = MoveToFront.transform(text, initialAlphabet);

Prevention

When it happens

Trigger: Calling transform("hello", null) or transform("hello", ""); passing an alphabet sourced from config that came back null or blank.

Common situations: Alphabet string read from a config/env var that was unset; alphabet defaulted to null; alphabet computed for a custom character set that produced an empty result.

Related errors


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