{"record":{"id":"5afdcaee0afd856f","repo":"TheAlgorithms/Java","slug":"basenumbers-must-be-non-empty","errorCode":null,"errorMessage":"baseNumbers must be non-empty.","messagePattern":"baseNumbers must be non-empty\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/NthUglyNumber.java","lineNumber":30,"sourceCode":" *   where the exponents a, b, c are non-negative integers.\n *   Some properties of ugly numbers:\n *     - base [2, 3, 5] ugly numbers are the 5-smooth numbers, cf. https://oeis.org/A051037\n *     - base [2, 3, 5, 7] ugly numbers are 7-smooth numbers, cf. https://oeis.org/A002473\n *     - base [2] ugly numbers are the non-negative powers of 2,\n *     - the base [2, 3, 5] ugly numbers are the same as base [5, 6, 2, 3, 5] ugly numbers\n */\npublic class NthUglyNumber {\n    private ArrayList<Long> uglyNumbers = new ArrayList<>(singletonList(1L));\n    private ArrayList<MutablePair<Integer, Integer>> positions = new ArrayList<>();\n\n    /**\n     * @brief initialized the object allowing to compute ugly numbers with given base\n     * @param baseNumbers the given base of ugly numbers\n     * @exception IllegalArgumentException baseNumber is empty\n     */\n    NthUglyNumber(final int[] baseNumbers) {\n        if (baseNumbers.length == 0) {\n            throw new IllegalArgumentException(\"baseNumbers must be non-empty.\");\n        }\n\n        for (final var baseNumber : baseNumbers) {\n            this.positions.add(MutablePair.of(baseNumber, 0));\n        }\n    }\n\n    /**\n     * @param n the zero-based-index of the queried ugly number\n     * @exception IllegalArgumentException n is negative\n     * @return the n-th ugly number (starting from index 0)\n     */\n    public Long get(final int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"n must be non-negative.\");\n        }\n\n        while (uglyNumbers.size() <= n) {","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/NthUglyNumber.java#L12-L48","documentation":"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.","triggerScenarios":"Constructing new NthUglyNumber(new int[0]) or new NthUglyNumber(new int[]{}) — passing an empty array of base numbers.","commonSituations":"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.","solutions":["Supply at least one base number, e.g. {2, 3, 5} for the classic ugly-number definition.","Validate baseNumbers.length > 0 at the source (config loader, parser) before constructing the object.","If the base set is dynamic, short-circuit with a sensible default when the input collection is empty."],"exampleFix":"// before\nint[] base = new int[0];\nNthUglyNumber u = new NthUglyNumber(base);\n\n// after\nint[] base = baseInput.length > 0 ? baseInput : new int[]{2, 3, 5};\nNthUglyNumber u = new NthUglyNumber(base);","handlingStrategy":"validation","validationCode":"if (baseNumbers == null || baseNumbers.length == 0) {\n    throw new IllegalArgumentException(\"baseNumbers must be non-empty\");\n}\nNthUglyNumber u = new NthUglyNumber(baseNumbers);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default the base set to {2, 3, 5} when the source collection is empty.","Validate config-driven arrays at load time rather than at construction time.","Treat an empty base array as a configuration error, not a runtime surprise."],"tags":["math","sequence","invalid-argument","empty-array"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}