TheAlgorithms/Java · error · IllegalArgumentException

Object distance must be non-zero.

Error message

Object distance must be non-zero.

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/physics/ThinLens.java:59

        return 1.0 / ((1.0 / focalLength) - (1.0 / objectDistance));
    }

    /**
     * Computes magnification of the image.
     *
     * <pre>
     *     m = v / u
     * </pre>
     *
     * @param imageDistance image distance (v)
     * @param objectDistance object distance (u)
     * @return magnification
     * @throws IllegalArgumentException if object distance is zero
     */
    public static double magnification(double imageDistance, double objectDistance) {

        if (objectDistance == 0) {
            throw new IllegalArgumentException("Object distance must be non-zero.");
        }

        return imageDistance / objectDistance;
    }

    /**
     * Determines whether the image formed is real or virtual.
     *
     * @param imageDistance image distance (v)
     * @return {@code true} if image is real, {@code false} if virtual
     */
    public static boolean isRealImage(double imageDistance) {
        return imageDistance > 0;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure objectDistance is a finite non-zero double before calling magnification.
  2. 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.
  3. Pair with imageDistance: fetch v from imageDistance() first and reuse the same non-zero objectDistance you passed there.

Example fix

// before
double m = ThinLens.magnification(v, objectDistance); // objectDistance could be 0.0

// after
double m = (objectDistance != 0.0)
    ? ThinLens.magnification(v, objectDistance)
    : throw new IllegalArgumentException("objectDistance must be non-zero");
Defensive patterns

Strategy: validation

Validate before calling

if (objectDistance == 0.0 || !Double.isFinite(objectDistance)) {
    throw new IllegalArgumentException("objectDistance must be finite and non-zero");
}
double m = ThinLens.magnification(imageDistance, objectDistance);

Type guard

static boolean validObjectDistance(double u) {
    return Double.isFinite(u) && u != 0.0;
}

Try / catch

try {
    double m = ThinLens.magnification(v, objectDistance);
} catch (IllegalArgumentException e) {
    logger.warn("Cannot compute magnification, objectDistance={}", objectDistance);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/68a1b2051e60df93. Report an issue: GitHub.