{"record":{"id":"68a1b2051e60df93","repo":"TheAlgorithms/Java","slug":"object-distance-must-be-non-zero","errorCode":null,"errorMessage":"Object distance must be non-zero.","messagePattern":"Object distance must be non-zero\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/physics/ThinLens.java","lineNumber":59,"sourceCode":"        return 1.0 / ((1.0 / focalLength) - (1.0 / objectDistance));\n    }\n\n    /**\n     * Computes magnification of the image.\n     *\n     * <pre>\n     *     m = v / u\n     * </pre>\n     *\n     * @param imageDistance image distance (v)\n     * @param objectDistance object distance (u)\n     * @return magnification\n     * @throws IllegalArgumentException if object distance is zero\n     */\n    public static double magnification(double imageDistance, double objectDistance) {\n\n        if (objectDistance == 0) {\n            throw new IllegalArgumentException(\"Object distance must be non-zero.\");\n        }\n\n        return imageDistance / objectDistance;\n    }\n\n    /**\n     * Determines whether the image formed is real or virtual.\n     *\n     * @param imageDistance image distance (v)\n     * @return {@code true} if image is real, {@code false} if virtual\n     */\n    public static boolean isRealImage(double imageDistance) {\n        return imageDistance > 0;\n    }\n}\n","sourceCodeStart":41,"sourceCodeEnd":75,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/physics/ThinLens.java#L41-L75","documentation":"Thrown by ThinLens.magnification(imageDistance, objectDistance) when objectDistance is exactly 0.0. Magnification m = v / u divides by the object distance, so a zero divisor would produce Infinity (or NaN); the guard rejects it instead of returning garbage.","triggerScenarios":"Call magnification(v, 0.0) for any v. Note imageDistance being 0 is allowed (it just yields magnification 0). Only objectDistance == 0 trips the check.","commonSituations":"Reusing an objectDistance variable that was reset to 0 between frames; computing objectDistance from a position difference that collapsed to zero (object at the lens); forgetting to set objectDistance after deserialization.","solutions":["Ensure objectDistance is a finite non-zero double before calling magnification.","If objectDistance legitimately approaches 0, cap it with a small epsilon (e.g. Math.abs(objectDistance) < 1e-9 ? EPS : objectDistance) appropriate to your unit system.","Pair with imageDistance: fetch v from imageDistance() first and reuse the same non-zero objectDistance you passed there."],"exampleFix":"// before\ndouble m = ThinLens.magnification(v, objectDistance); // objectDistance could be 0.0\n\n// after\ndouble m = (objectDistance != 0.0)\n    ? ThinLens.magnification(v, objectDistance)\n    : throw new IllegalArgumentException(\"objectDistance must be non-zero\");","handlingStrategy":"validation","validationCode":"if (objectDistance == 0.0 || !Double.isFinite(objectDistance)) {\n    throw new IllegalArgumentException(\"objectDistance must be finite and non-zero\");\n}\ndouble m = ThinLens.magnification(imageDistance, objectDistance);","typeGuard":"static boolean validObjectDistance(double u) {\n    return Double.isFinite(u) && u != 0.0;\n}","tryCatchPattern":"try {\n    double m = ThinLens.magnification(v, objectDistance);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"Cannot compute magnification, objectDistance={}\", objectDistance);\n}","preventionTips":["Reuse the same validated objectDistance for both imageDistance and magnification.","Add an epsilon floor if your physics tolerates near-zero distances.","Treat objectDistance as derived from a non-degenerate position delta."],"tags":["physics","input-validation","illegal-argument","divide-by-zero","thin-lens"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}