TheAlgorithms/Java · error · IllegalArgumentException

Input string cannot be null or empty.

Error message

Input string cannot be null or empty.

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/compression/ArithmeticCoding.java:56

 * Arithmetic coding</a></li>
 * </ul>
 * </p>
 */
public final class ArithmeticCoding {

    private ArithmeticCoding() {
    }

    /**
     * Compresses a string using the Arithmetic Coding algorithm.
     *
     * @param uncompressed The string to be compressed.
     * @return The compressed representation as a BigDecimal number.
     * @throws IllegalArgumentException if the input string is null or empty.
     */
    public static BigDecimal compress(String uncompressed) {
        if (uncompressed == null || uncompressed.isEmpty()) {
            throw new IllegalArgumentException("Input string cannot be null or empty.");
        }

        Map<Character, Symbol> probabilityTable = calculateProbabilities(uncompressed);

        BigDecimal low = BigDecimal.ZERO;
        BigDecimal high = BigDecimal.ONE;

        for (char symbol : uncompressed.toCharArray()) {
            BigDecimal range = high.subtract(low);
            Symbol sym = probabilityTable.get(symbol);

            high = low.add(range.multiply(sym.high()));
            low = low.add(range.multiply(sym.low()));
        }

        return low; // Return the lower bound of the final interval
    }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure the input string is non-null and contains at least one character before compressing.
  2. Handle empty input as a special case upstream rather than passing it to compress.
  3. Null-check data read from external sources before invoking the compressor.

Example fix

// before
BigDecimal out = ArithmeticCoding.compress(payload);

// after
if (payload == null || payload.isEmpty()) {
    throw new IllegalArgumentException("Nothing to compress: payload is null or empty");
}
BigDecimal out = ArithmeticCoding.compress(payload);
Defensive patterns

Strategy: validation

Validate before calling

if (uncompressed == null || uncompressed.isEmpty()) {
    throw new IllegalArgumentException("Input to ArithmeticCoding.compress must be non-empty");
}
BigDecimal out = ArithmeticCoding.compress(uncompressed);

Prevention

When it happens

Trigger: Calling ArithmeticCoding.compress(null) or ArithmeticCoding.compress("").

Common situations: Input read from a file that was empty; a string field that was never populated; a pipeline stage produced null and forwarded it.

Related errors


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