{"record":{"id":"84faa2633fe2593f","repo":"TheAlgorithms/Java","slug":"state-must-be-array-of-length-2","errorCode":null,"errorMessage":"State must be array of length 2","messagePattern":"State must be array of length 2","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/physics/SimplePendulumRK4.java","lineNumber":59,"sourceCode":"     */\n    private double[] derivatives(double[] state) {\n        double theta = state[0];\n        double omega = state[1];\n        double dtheta = omega;\n        double domega = -(g / length) * Math.sin(theta);\n        return new double[] {dtheta, domega};\n    }\n\n    /**\n     * Performs one time step using the RK4 method.\n     *\n     * @param state the current state [theta, omega]\n     * @param dt the time step size\n     * @return the new state after time dt\n     */\n    public double[] stepRK4(double[] state, double dt) {\n        if (state == null || state.length != 2) {\n            throw new IllegalArgumentException(\"State must be array of length 2\");\n        }\n        if (dt <= 0) {\n            throw new IllegalArgumentException(\"Time step must be positive\");\n        }\n\n        double[] k1 = derivatives(state);\n        double[] s2 = new double[] {state[0] + 0.5 * dt * k1[0], state[1] + 0.5 * dt * k1[1]};\n\n        double[] k2 = derivatives(s2);\n        double[] s3 = new double[] {state[0] + 0.5 * dt * k2[0], state[1] + 0.5 * dt * k2[1]};\n\n        double[] k3 = derivatives(s3);\n        double[] s4 = new double[] {state[0] + dt * k3[0], state[1] + dt * k3[1]};\n\n        double[] k4 = derivatives(s4);\n\n        double thetaNext = state[0] + dt / 6.0 * (k1[0] + 2 * k2[0] + 2 * k3[0] + k4[0]);\n        double omegaNext = state[1] + dt / 6.0 * (k1[1] + 2 * k2[1] + 2 * k3[1] + k4[1]);","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/physics/SimplePendulumRK4.java#L41-L77","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ndouble[] state = new double[]{0.1};\ndouble[] next = p.stepRK4(state, 0.01);\n// after\ndouble[] state = new double[]{0.1, 0.0};\ndouble[] next = p.stepRK4(state, 0.01);","handlingStrategy":"type-guard","validationCode":"double[] state = (s == null || s.length != 2) ? new double[]{0.0, 0.0} : s;\ndouble[] next = p.stepRK4(state, dt);","typeGuard":"static boolean isValidPendulumState(double[] s) {\n    return s != null && s.length == 2;\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["java","physics","parameter-validation","numerical-integration","rk4","state-vector","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}