TheAlgorithms/Java · error · IllegalArgumentException

baseNumbers must be non-empty.

Error message

baseNumbers must be non-empty.

What it means

Thrown by the NthUglyNumber constructor when baseNumbers is an empty int[]. Ugly numbers are defined relative to a set of prime bases; an empty base set makes the sequence undefined (there would be no candidates to generate). The constructor guards against this before initializing the positions list.

Solutions

  1. Supply at least one base number, e.g. {2, 3, 5} for the classic ugly-number definition.
  2. Validate baseNumbers.length > 0 at the source (config loader, parser) before constructing the object.
  3. If the base set is dynamic, short-circuit with a sensible default when the input collection is empty.

Example fix

// before
int[] base = new int[0];
NthUglyNumber u = new NthUglyNumber(base);

// after
int[] base = baseInput.length > 0 ? baseInput : new int[]{2, 3, 5};
NthUglyNumber u = new NthUglyNumber(base);
Defensive patterns

Strategy: validation

Validate before calling

if (baseNumbers == null || baseNumbers.length == 0) {
    throw new IllegalArgumentException("baseNumbers must be non-empty");
}
NthUglyNumber u = new NthUglyNumber(baseNumbers);

Prevention

When it happens

Trigger: Constructing new NthUglyNumber(new int[0]) or new NthUglyNumber(new int[]{}) — passing an empty array of base numbers.

Common situations: baseNumbers is loaded from configuration, a database column, or user input that was empty; a filter/map chain upstream reduced the array to zero elements; default-initializing the field to an empty array as a placeholder.

Related errors


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

Appendix: source

Thrown at src/main/java/com/thealgorithms/maths/NthUglyNumber.java:30

 *   where the exponents a, b, c are non-negative integers.
 *   Some properties of ugly numbers:
 *     - base [2, 3, 5] ugly numbers are the 5-smooth numbers, cf. https://oeis.org/A051037
 *     - base [2, 3, 5, 7] ugly numbers are 7-smooth numbers, cf. https://oeis.org/A002473
 *     - base [2] ugly numbers are the non-negative powers of 2,
 *     - the base [2, 3, 5] ugly numbers are the same as base [5, 6, 2, 3, 5] ugly numbers
 */
public class NthUglyNumber {
    private ArrayList<Long> uglyNumbers = new ArrayList<>(singletonList(1L));
    private ArrayList<MutablePair<Integer, Integer>> positions = new ArrayList<>();

    /**
     * @brief initialized the object allowing to compute ugly numbers with given base
     * @param baseNumbers the given base of ugly numbers
     * @exception IllegalArgumentException baseNumber is empty
     */
    NthUglyNumber(final int[] baseNumbers) {
        if (baseNumbers.length == 0) {
            throw new IllegalArgumentException("baseNumbers must be non-empty.");
        }

        for (final var baseNumber : baseNumbers) {
            this.positions.add(MutablePair.of(baseNumber, 0));
        }
    }

    /**
     * @param n the zero-based-index of the queried ugly number
     * @exception IllegalArgumentException n is negative
     * @return the n-th ugly number (starting from index 0)
     */
    public Long get(final int n) {
        if (n < 0) {
            throw new IllegalArgumentException("n must be non-negative.");
        }

        while (uglyNumbers.size() <= n) {

View on GitHub (pinned to fdfb9a395b)