TheAlgorithms/Java · error · IllegalArgumentException
State must be array of length 2
Error message
State must be array of length 2
What it means
Thrown by SimplePendulumRK4.stepRK4 when the state array is null or its length is not exactly 2. The RK4 step expects a state vector [theta, omega] (angle, angular velocity) to compute four intermediate slopes; any other shape would cause index-out-of-bounds or null-dereference. This is the first guard, checked before the dt guard.
Source
Thrown at src/main/java/com/thealgorithms/physics/SimplePendulumRK4.java:59
*/
private double[] derivatives(double[] state) {
double theta = state[0];
double omega = state[1];
double dtheta = omega;
double domega = -(g / length) * Math.sin(theta);
return new double[] {dtheta, domega};
}
/**
* Performs one time step using the RK4 method.
*
* @param state the current state [theta, omega]
* @param dt the time step size
* @return the new state after time dt
*/
public double[] stepRK4(double[] state, double dt) {
if (state == null || state.length != 2) {
throw new IllegalArgumentException("State must be array of length 2");
}
if (dt <= 0) {
throw new IllegalArgumentException("Time step must be positive");
}
double[] k1 = derivatives(state);
double[] s2 = new double[] {state[0] + 0.5 * dt * k1[0], state[1] + 0.5 * dt * k1[1]};
double[] k2 = derivatives(s2);
double[] s3 = new double[] {state[0] + 0.5 * dt * k2[0], state[1] + 0.5 * dt * k2[1]};
double[] k3 = derivatives(s3);
double[] s4 = new double[] {state[0] + dt * k3[0], state[1] + dt * k3[1]};
double[] k4 = derivatives(s4);
double thetaNext = state[0] + dt / 6.0 * (k1[0] + 2 * k2[0] + 2 * k3[0] + k4[0]);
double omegaNext = state[1] + dt / 6.0 * (k1[1] + 2 * k2[1] + 2 * k3[1] + k4[1]);View on GitHub (pinned to fdfb9a395b)
Solutions
- Always pass a 2-element double[] of the form [theta, omega], e.g. new double[]{0.1, 0.0} for a small initial angle at rest.
- If you carry a longer state elsewhere, copy the [theta, omega] slice into a length-2 array before stepping.
- Initialize the state at simulation start rather than reusing a stale reference.
Example fix
// before
double[] state = new double[]{0.1};
double[] next = p.stepRK4(state, 0.01);
// after
double[] state = new double[]{0.1, 0.0};
double[] next = p.stepRK4(state, 0.01); Defensive patterns
Strategy: type-guard
Validate before calling
double[] state = (s == null || s.length != 2) ? new double[]{0.0, 0.0} : s;
double[] next = p.stepRK4(state, dt); Type guard
static boolean isValidPendulumState(double[] s) {
return s != null && s.length == 2;
} Prevention
- Standardise on the [theta, omega] layout across your pendulum module.
- Initialize the state with explicit literals new double[]{theta0, 0.0}.
- Copy slices from longer states rather than passing them directly.
When it happens
Trigger: Calling stepRK4(null, dt), stepRK4(new double[]{theta}, dt) (length 1), or stepRK4 with a state array that came from a different integrator using a longer layout.
Common situations: Mixing state arrays between the pendulum integrator and another physics module, a null state left over after a failed init, or a copy/truncate that dropped an element.
Related errors
- State must be a non-null array of length 2.
- Time step must be positive
- Time step must be positive.
- Orbiting mass and radius must be positive.
- Natural frequency must be positive.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/84faa2633fe2593f.
Report an issue: GitHub.