{"record":{"id":"f8edd6dea8661d80","repo":"TheAlgorithms/Java","slug":"input-string-cannot-be-null-or-empty","errorCode":null,"errorMessage":"Input string cannot be null or empty.","messagePattern":"Input string cannot be null or empty\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/compression/ArithmeticCoding.java","lineNumber":56,"sourceCode":" * Arithmetic coding</a></li>\n * </ul>\n * </p>\n */\npublic final class ArithmeticCoding {\n\n    private ArithmeticCoding() {\n    }\n\n    /**\n     * Compresses a string using the Arithmetic Coding algorithm.\n     *\n     * @param uncompressed The string to be compressed.\n     * @return The compressed representation as a BigDecimal number.\n     * @throws IllegalArgumentException if the input string is null or empty.\n     */\n    public static BigDecimal compress(String uncompressed) {\n        if (uncompressed == null || uncompressed.isEmpty()) {\n            throw new IllegalArgumentException(\"Input string cannot be null or empty.\");\n        }\n\n        Map<Character, Symbol> probabilityTable = calculateProbabilities(uncompressed);\n\n        BigDecimal low = BigDecimal.ZERO;\n        BigDecimal high = BigDecimal.ONE;\n\n        for (char symbol : uncompressed.toCharArray()) {\n            BigDecimal range = high.subtract(low);\n            Symbol sym = probabilityTable.get(symbol);\n\n            high = low.add(range.multiply(sym.high()));\n            low = low.add(range.multiply(sym.low()));\n        }\n\n        return low; // Return the lower bound of the final interval\n    }\n","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/compression/ArithmeticCoding.java#L38-L74","documentation":"ArithmeticCoding.compress(String) rejects null or empty input because the algorithm builds a probability table over the characters and iterates them to narrow an interval — with no characters there is no probability distribution and no meaningful compressed value (BigDecimal low/high never move). The guard prevents a degenerate result.","triggerScenarios":"Calling ArithmeticCoding.compress(null) or ArithmeticCoding.compress(\"\").","commonSituations":"Input read from a file that was empty; a string field that was never populated; a pipeline stage produced null and forwarded it.","solutions":["Ensure the input string is non-null and contains at least one character before compressing.","Handle empty input as a special case upstream rather than passing it to compress.","Null-check data read from external sources before invoking the compressor."],"exampleFix":"// before\nBigDecimal out = ArithmeticCoding.compress(payload);\n\n// after\nif (payload == null || payload.isEmpty()) {\n    throw new IllegalArgumentException(\"Nothing to compress: payload is null or empty\");\n}\nBigDecimal out = ArithmeticCoding.compress(payload);","handlingStrategy":"validation","validationCode":"if (uncompressed == null || uncompressed.isEmpty()) {\n    throw new IllegalArgumentException(\"Input to ArithmeticCoding.compress must be non-empty\");\n}\nBigDecimal out = ArithmeticCoding.compress(uncompressed);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Handle empty/null input as a special case upstream.","Null-check data from external sources before compressing.","Guard pipeline stages that could forward null."],"tags":["compression","arithmetic-coding","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}