{"record":{"id":"21744cc00f3c8748","repo":"TheAlgorithms/Java","slug":"alphabet-cannot-be-null-or-empty-when-text-is-not","errorCode":null,"errorMessage":"Alphabet cannot be null or empty when text is not empty.","messagePattern":"Alphabet cannot be null or empty when text is not empty\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/compression/MoveToFront.java","lineNumber":96,"sourceCode":"     * all unique characters that may appear in the input.</p>\n     *\n     * @param text the input string to transform; if empty, returns an empty list\n     * @param initialAlphabet a string containing the initial ordered set of symbols\n     *                        (e.g., \"$abn\" or the full ASCII set); must not be empty\n     *                        when {@code text} is non-empty\n     * @return a list of integers representing the transformed data, where each integer\n     *         is the index of the corresponding input character in the current alphabet state\n     * @throws IllegalArgumentException if {@code text} is non-empty and {@code initialAlphabet}\n     *                                  is {@code null} or empty\n     * @throws IllegalArgumentException if any character in {@code text} is not found in\n     *                                  {@code initialAlphabet}\n     */\n    public static List<Integer> transform(String text, String initialAlphabet) {\n        if (text == null || text.isEmpty()) {\n            return new ArrayList<>();\n        }\n        if (initialAlphabet == null || initialAlphabet.isEmpty()) {\n            throw new IllegalArgumentException(\"Alphabet cannot be null or empty when text is not empty.\");\n        }\n\n        List<Integer> output = new ArrayList<>(text.length());\n\n        // Use LinkedList for O(1) add-to-front and O(n) remove operations\n        // This is more efficient than ArrayList for the move-to-front pattern\n        List<Character> alphabet = initialAlphabet.chars().mapToObj(c -> (char) c).collect(Collectors.toCollection(LinkedList::new));\n\n        for (char c : text.toCharArray()) {\n            int index = alphabet.indexOf(c);\n            if (index == -1) {\n                throw new IllegalArgumentException(\"Symbol '\" + c + \"' not found in the initial alphabet.\");\n            }\n\n            output.add(index);\n\n            // Move the character to the front\n            Character symbol = alphabet.remove(index);","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/compression/MoveToFront.java#L78-L114","documentation":"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.","triggerScenarios":"Calling transform(\"hello\", null) or transform(\"hello\", \"\"); passing an alphabet sourced from config that came back null or blank.","commonSituations":"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.","solutions":["Pass a non-empty alphabet such as \"abcdefghijklmnopqrstuvwxyz\" that contains every character in the text.","Validate the alphabet is non-null and non-empty before calling transform when the text is non-empty.","Derive the alphabet from the text's distinct characters as a fallback."],"exampleFix":"// before\nList<Integer> out = MoveToFront.transform(text, alphabet); // alphabet null\n\n// after\nif (alphabet == null || alphabet.isEmpty()) {\n    alphabet = text.chars().distinct()\n        .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)\n        .toString();\n}\nList<Integer> out = MoveToFront.transform(text, alphabet);","handlingStrategy":"validation","validationCode":"if (text != null && !text.isEmpty() && (initialAlphabet == null || initialAlphabet.isEmpty())) {\n    throw new IllegalArgumentException(\"Alphabet must be non-empty when text is non-empty\");\n}\nList<Integer> out = MoveToFront.transform(text, initialAlphabet);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass an alphabet that contains every character in the text.","Derive the alphabet from the text's distinct characters if none is configured.","Validate alphabet presence whenever the text is non-empty."],"tags":["compression","move-to-front","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}