{"record":{"id":"c3e3f72df1d853d5","repo":"TheAlgorithms/Java","slug":"length-must-be-positive","errorCode":null,"errorMessage":"Length must be positive","messagePattern":"Length must be positive","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/physics/SimplePendulumRK4.java","lineNumber":26,"sourceCode":" */\npublic final class SimplePendulumRK4 {\n\n    private SimplePendulumRK4() {\n        throw new AssertionError(\"No instances.\");\n    }\n\n    private final double length; // meters\n    private final double g; // acceleration due to gravity (m/s^2)\n\n    /**\n     * Constructs a simple pendulum simulator.\n     *\n     * @param length the length of the pendulum in meters\n     * @param g the acceleration due to gravity in m/s^2\n     */\n    public SimplePendulumRK4(double length, double g) {\n        if (length <= 0) {\n            throw new IllegalArgumentException(\"Length must be positive\");\n        }\n        if (g <= 0) {\n            throw new IllegalArgumentException(\"Gravity must be positive\");\n        }\n        this.length = length;\n        this.g = g;\n    }\n\n    /**\n     * Computes the derivatives of the state vector.\n     * State: [theta, omega] where theta is angle and omega is angular velocity.\n     *\n     * @param state the current state [theta, omega]\n     * @return the derivatives [dtheta/dt, domega/dt]\n     */\n    private double[] derivatives(double[] state) {\n        double theta = state[0];\n        double omega = state[1];","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/physics/SimplePendulumRK4.java#L8-L44","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nSimplePendulumRK4 p = new SimplePendulumRK4(0, 9.81);\n// after\nSimplePendulumRK4 p = new SimplePendulumRK4(1.0, 9.81);","handlingStrategy":"validation","validationCode":"if (Double.isNaN(length) || length <= 0) {\n    throw new IllegalArgumentException(\"length must be > 0\");\n}\nSimplePendulumRK4 p = new SimplePendulumRK4(length, g);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["java","physics","parameter-validation","pendulum","constructor","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}