{"record":{"id":"26146da43ec98b57","repo":"TheAlgorithms/Java","slug":"gravity-must-be-positive","errorCode":null,"errorMessage":"Gravity must be positive","messagePattern":"Gravity must be positive","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/physics/SimplePendulumRK4.java","lineNumber":29,"sourceCode":"    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];\n        double dtheta = omega;\n        double domega = -(g / length) * Math.sin(theta);\n        return new double[] {dtheta, domega};","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/physics/SimplePendulumRK4.java#L11-L47","documentation":"Thrown by the SimplePendulumRK4 constructor when g <= 0. The gravitational acceleration g appears in omega = sqrt(g / L) and drives the restoring torque; a non-positive g makes the pendulum non-oscillatory or undefined. This is the second guard, checked after the length guard.","triggerScenarios":"Calling new SimplePendulumRK4(length, g) with g == 0 or a negative g. Using g of exactly 0 models free-fall (no oscillation) and is rejected; ~9.81 m/s^2 is the Earth-surface convention.","commonSituations":"Config defaulting g to 0, modelling a different body but forgetting to set g (leaving 0), or a unit error (g given in cm/s^2 and then divided).","solutions":["Pass a strictly positive g in m/s^2 (9.81 Earth, 1.62 Moon, 3.71 Mars).","Make g a named constant or config value with validation at load time.","If switching celestial bodies, ensure the g field is reassigned, not left at a 0 default."],"exampleFix":"// before\nSimplePendulumRK4 p = new SimplePendulumRK4(1.0, 0);\n// after\nSimplePendulumRK4 p = new SimplePendulumRK4(1.0, 9.81);","handlingStrategy":"validation","validationCode":"if (Double.isNaN(g) || g <= 0) {\n    throw new IllegalArgumentException(\"g must be > 0\");\n}\nSimplePendulumRK4 p = new SimplePendulumRK4(length, g);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Source g from a named constant (EARTH_G = 9.81) rather than a literal that may default to 0.","Reassign g when switching celestial bodies; do not leave it at 0.","Validate g alongside length in one preconstruction check."],"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"}