TheAlgorithms/Java · error · RuntimeException
Number must be zero for E assignment!
Error message
Number must be zero for E assignment!
What it means
Thrown by Builder.e() when the builder's internal number field is not 0. Same assignment-vs-accumulation pattern as pi() and rand(): e() overwrites number with Math.E and requires a clean slate. Uses RuntimeException.
Source
Thrown at src/main/java/com/thealgorithms/maths/MathBuilder.java:129
}
Random random = new Random();
number = random.nextDouble(seed);
return this;
}
// Takes PI value and sets to NUMBER
public Builder pi() {
if (number != 0) {
throw new RuntimeException("Number must be zero for PI assignment!");
}
number = Math.PI;
return this;
}
// Takes E value and sets to NUMBER
public Builder e() {
if (number != 0) {
throw new RuntimeException("Number must be zero for E assignment!");
}
number = Math.E;
return this;
}
public Builder randomInRange(double min, double max) {
if (number != 0) {
throw new RuntimeException("Number must be zero for random assignment!");
}
Random random = new Random();
number = min + (max - min) * random.nextDouble();
return this;
}
public Builder toDegrees() {
if (inParenthesis) {
sideNumber = Math.toDegrees(sideNumber);
} else {View on GitHub (pinned to fdfb9a395b)
Solutions
- Call e() on a fresh Builder (number == 0)
- Use multiply(Math.E) or add(Math.E) if you want E in an arithmetic expression
- Build current state first, then start fresh for the E assignment
Example fix
// before MathBuilder.Builder b = new MathBuilder.Builder(2).e(); // after MathBuilder.Builder b = new MathBuilder.Builder().e(); // or for arithmetic with E: MathBuilder.Builder b2 = new MathBuilder.Builder(2).multiply(Math.E);
Defensive patterns
Strategy: validation
Validate before calling
// Call e() only on a fresh builder MathBuilder.Builder b = new MathBuilder.Builder(); // number == 0 b.e();
Try / catch
try {
builder.e();
} catch (RuntimeException e) {
builder = new MathBuilder.Builder();
builder.e();
} Prevention
- e() overwrites number with Math.E — use multiply(Math.E) for arithmetic instead
- Only call e() when number is in its initial 0 state
- Do not chain e() after accumulating operations like add or multiply
When it happens
Trigger: Calling e() after any operation that left number non-zero: new MathBuilder.Builder(3).e(), or after add/multiply/set on a non-zero builder.
Common situations: Developer chains operations expecting e() to participate in arithmetic (e.g., multiply by e), but the API treats it as a raw overwrite. Confusion about builder semantics.
Related errors
- Number must be zero for random assignment!
- Number must be zero for PI assignment!
- Number must be zero to set!
- 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/7b1294ea097fe88b.
Report an issue: GitHub.