{"record":{"id":"7d09362f13dd1ff1","repo":"TheAlgorithms/Java","slug":"state-must-be-a-non-null-array-of-length-2","errorCode":null,"errorMessage":"State must be a non-null array of length 2.","messagePattern":"State must be a non-null array of length 2\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/physics/DampedOscillator.java","lineNumber":84,"sourceCode":"     * @return the displacement x(t)\n     */\n    public double displacementAnalytical(double amplitude, double phase, double time) {\n        double omegaD = Math.sqrt(Math.max(0.0, omega0 * omega0 - gamma * gamma));\n        return amplitude * Math.exp(-gamma * time) * Math.cos(omegaD * time + phase);\n    }\n\n    /**\n     * Performs a single integration step using the explicit Euler method.\n     * State vector format: [x, v], where v = dx/dt.\n     *\n     * @param state the current state [x, v]\n     * @param dt    the time step (seconds)\n     * @return the next state [x_next, v_next]\n     * @throws IllegalArgumentException if the state array is invalid or dt is non-positive\n     */\n    public double[] stepEuler(double[] state, double dt) {\n        if (state == null || state.length != 2) {\n            throw new IllegalArgumentException(\"State must be a non-null array of length 2.\");\n        }\n        if (dt <= 0) {\n            throw new IllegalArgumentException(\"Time step must be positive.\");\n        }\n\n        double x = state[0];\n        double v = state[1];\n        double acceleration = -2.0 * gamma * v - omega0 * omega0 * x;\n\n        double xNext = x + dt * v;\n        double vNext = v + dt * acceleration;\n\n        return new double[] {xNext, vNext};\n    }\n\n    /** @return the natural (undamped) angular frequency (rad/s). */\n    public double getOmega0() {\n        return omega0;","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/physics/DampedOscillator.java#L66-L102","documentation":"Thrown by DampedOscillator.stepEuler when the state array is null or its length is not exactly 2. The explicit Euler step expects a 2-element state vector [x, v] (displacement, velocity); any other shape would cause an ArrayIndexOutOfBoundsException or a NullPointerException on state[0]/state[1]. This is the first of two guards in stepEuler (the second validates dt).","triggerScenarios":"Calling stepEuler(null, dt), stepEuler(new double[]{x}, dt) (length 1), stepEuler(new double[]{x, v, a}, dt) (length 3), or passing a state array from a different integrator that uses a different state layout.","commonSituations":"Mixing state representations across integrators (e.g. passing a 4-element RK state to the Euler step), reusing an array that was truncated, or a null state after a failed initialization loop.","solutions":["Always pass a 2-element double[] of the form [displacement, velocity].","If you maintain a longer state elsewhere (e.g. for other physics), copy the relevant [x, v] slice into a length-2 array before stepping.","Initialize state with new double[]{0.0, 0.0} at the start of a simulation rather than leaving it null."],"exampleFix":"// before\ndouble[] state = new double[]{1.0};\ndouble[] next = osc.stepEuler(state, 0.01);\n// after\ndouble[] state = new double[]{1.0, 0.0};\ndouble[] next = osc.stepEuler(state, 0.01);","handlingStrategy":"type-guard","validationCode":"double[] state = (s == null || s.length != 2) ? new double[]{0.0, 0.0} : s;\ndouble[] next = osc.stepEuler(state, dt);","typeGuard":"static boolean isValidEulerState(double[] s) {\n    return s != null && s.length == 2;\n}","tryCatchPattern":null,"preventionTips":["Standardise on a 2-element [x, v] state throughout your simulation module.","Initialize state arrays with explicit literals new double[]{x0, v0}.","When interoperating with other integrators, copy slices into the expected layout."],"tags":["java","physics","parameter-validation","numerical-integration","state-vector","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}