TheAlgorithms/Java · error · IllegalArgumentException
Length must be positive
Error message
Length must be positive
What it means
Thrown by the SimplePendulumRK4 constructor when length <= 0. The pendulum's angular frequency is omega = sqrt(g / L); a non-positive length makes this undefined or imaginary and is physically meaningless. This is the first guard, checked before the gravity guard in the same constructor.
Source
Thrown at src/main/java/com/thealgorithms/physics/SimplePendulumRK4.java:26
*/
public final class SimplePendulumRK4 {
private SimplePendulumRK4() {
throw new AssertionError("No instances.");
}
private final double length; // meters
private final double g; // acceleration due to gravity (m/s^2)
/**
* Constructs a simple pendulum simulator.
*
* @param length the length of the pendulum in meters
* @param g the acceleration due to gravity in m/s^2
*/
public SimplePendulumRK4(double length, double g) {
if (length <= 0) {
throw new IllegalArgumentException("Length must be positive");
}
if (g <= 0) {
throw new IllegalArgumentException("Gravity must be positive");
}
this.length = length;
this.g = g;
}
/**
* Computes the derivatives of the state vector.
* State: [theta, omega] where theta is angle and omega is angular velocity.
*
* @param state the current state [theta, omega]
* @return the derivatives [dtheta/dt, domega/dt]
*/
private double[] derivatives(double[] state) {
double theta = state[0];
double omega = state[1];View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass a strictly positive length in meters (e.g. 1.0 for a typical lab pendulum).
- Validate length at the configuration boundary and reject zero/negative values upstream.
- Guard NaN explicitly if length is computed: require !Double.isNaN(length) && length > 0.
Example fix
// before SimplePendulumRK4 p = new SimplePendulumRK4(0, 9.81); // after SimplePendulumRK4 p = new SimplePendulumRK4(1.0, 9.81);
Defensive patterns
Strategy: validation
Validate before calling
if (Double.isNaN(length) || length <= 0) {
throw new IllegalArgumentException("length must be > 0");
}
SimplePendulumRK4 p = new SimplePendulumRK4(length, g); Prevention
- Validate length at config load time.
- Note the constructor does not reject NaN — add an explicit NaN check.
- Use canonical lab values (1.0 m) in tests to catch default-zero bugs.
When it happens
Trigger: Calling new SimplePendulumRK4(length, g) with length == 0, a negative length, or (commonly) a length field that was never initialised. Note: NaN length will NOT trip this guard (NaN <= 0 is false) and will surface later.
Common situations: Uninitialised length defaulting to 0, a unit-conversion error (cm passed where m expected and divided again), or a measurement of zero from a broken sensor.
Related errors
- Gravity must be positive
- Natural frequency must be positive.
- Damping coefficient must be non-negative.
- Orbiting mass and radius must be positive.
- State must be a non-null array of length 2.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/c3e3f72df1d853d5.
Report an issue: GitHub.