TheAlgorithms/Java · error · RuntimeException
Number must be zero to set!
Error message
Number must be zero to set!
What it means
Thrown by Builder.set(double num) when the builder's internal number field is not 0. The set() method is an unconditional assignment that replaces number with the given value; the guard prevents accidental overwriting of accumulated state. Uses RuntimeException.
Source
Thrown at src/main/java/com/thealgorithms/maths/MathBuilder.java:426
return this;
}
// Recalls the NUMBER on condition
public Builder recallIf(Function<Double, Boolean> condition, boolean cleanMemory) {
if (!condition.apply(number)) {
return this;
}
number = memory;
if (cleanMemory) {
memory = 0;
}
return this;
}
// Replaces NUMBER with given number
public Builder set(double num) {
if (number != 0) {
throw new RuntimeException("Number must be zero to set!");
}
number = num;
return this;
}
// Replaces NUMBER with given number on condition
public Builder setIf(double num, BiFunction<Double, Double, Boolean> condition) {
if (number != 0) {
throw new RuntimeException("Number must be zero to set!");
}
if (condition.apply(number, num)) {
number = num;
}
return this;
}
// Prints current NUMBER
public Builder print() {View on GitHub (pinned to fdfb9a395b)
Solutions
- Call set() only on a fresh Builder where number is 0
- Create a new Builder() instance for each independent computation rather than reusing one
- If you must reset, build the current state first, then construct a new Builder for the next value
Example fix
// before MathBuilder.Builder b = new MathBuilder.Builder(5); b.set(10); // throws // after MathBuilder.Builder b = new MathBuilder.Builder(); b.set(10);
Defensive patterns
Strategy: validation
Validate before calling
// Call set() only on a fresh builder where number == 0 MathBuilder.Builder b = new MathBuilder.Builder(); b.set(42.0);
Try / catch
try {
builder.set(value);
} catch (RuntimeException e) {
builder = new MathBuilder.Builder();
builder.set(value);
} Prevention
- set() requires number == 0 despite its name implying unconditional overwrite
- Never reuse a Builder after build() — create a new one for the next computation
- For conditional assignment use setIf, but it has the same number == 0 precondition
When it happens
Trigger: Calling set(num) after any operation that set number to non-zero: new MathBuilder.Builder(5).set(10), or after add/multiply/pi/rand on a non-zero builder.
Common situations: Developer expects set() to work like a setter (always overwrite), but the guard requires number to be 0 first. This is counterintuitive since set implies overwrite semantics. Typically hit when reusing a Builder across multiple computations.
Related errors
- Number must be zero for random assignment!
- Number must be zero for PI assignment!
- Number must be zero for E assignment!
- Cannot convert NaN to long!
- Theta (angle) must be a finite number.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/25c467c80d18a43a.
Report an issue: GitHub.