microg/GmsCore · error · NullPointerException

null camera target

Error message

null camera target

What it means

The CameraPosition(LatLng, float, float, float) constructor throws NullPointerException immediately when the target LatLng is null. The library requires every camera position to point at a real geographic coordinate, so a null target is rejected at construction time rather than failing later during rendering. This is an eager argument check, so the exception surfaces at the exact call site that built the bad CameraPosition.

Source

Thrown at play-services-maps/src/main/java/com/google/android/gms/maps/model/CameraPosition.java:84

    /**
     * Constructs a CameraPosition.
     *
     * @param target  The target location to align with the center of the screen.
     * @param zoom    Zoom level at target. See {@link #zoom} for details of restrictions.
     * @param tilt    The camera angle, in degrees, from the nadir (directly down). See
     *                {@link #tilt} for details of restrictions.
     * @param bearing Direction that the camera is pointing in, in degrees clockwise from north.
     *                This value will be normalized to be within 0 degrees inclusive and 360
     *                degrees exclusive.
     * @throws NullPointerException     if {@code target} is {@code null}
     * @throws IllegalArgumentException if {@code tilt} is outside range of {@code 0} to {@code 90}
     *                                  degrees inclusive
     */
    public CameraPosition(LatLng target, float zoom, float tilt, float bearing)
            throws NullPointerException, IllegalArgumentException {
        if (target == null) {
            throw new NullPointerException("null camera target");
        }
        this.target = target;
        this.zoom = zoom;
        if (tilt < 0 || 90 < tilt) {
            throw new IllegalArgumentException("Tilt needs to be between 0 and 90 inclusive");
        }
        this.tilt = tilt;
        if (bearing <= 0) {
            bearing += 360;
        }
        this.bearing = bearing % 360;
    }

    /**
     * Creates a builder for a camera position.
     */
    public static Builder builder() {
        return new Builder();

View on GitHub (pinned to 157c9d86ac)

Solutions

  1. Check the target LatLng for null before constructing the CameraPosition and use a sensible default position when it is null.
  2. If using CameraPosition.Builder, always call target(...) before build(); otherwise use the default CameraPosition directly.
  3. When deserializing saved camera state, validate all fields exist and are non-null before reconstructing the position.
  4. Wrap construction in try-catch as a last resort and fall back to a default camera position.

Example fix

// before
cameraUpdate = CameraUpdateFactory.newCameraPosition(
    new CameraPosition(myLocation, 15f, 45f, 0f));

// after
if (myLocation != null) {
    cameraUpdate = CameraUpdateFactory.newCameraPosition(
        new CameraPosition(myLocation, 15f, 45f, 0f));
} else {
    cameraUpdate = CameraUpdateFactory.newCameraPosition(
        new CameraPosition(DEFAULT_TARGET, 15f, 45f, 0f));
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (target == null) { target = DEFAULT_TARGET; }

Type guard

boolean isValidTarget(LatLng t) { return t != null; }

Try / catch

try {
    CameraPosition pos = new CameraPosition(target, zoom, tilt, bearing);
} catch (NullPointerException e) {
    camera.moveCamera(CameraUpdateFactory.newCameraPosition(DEFAULT_POSITION));
}

Prevention

When it happens

Trigger: Calling new CameraPosition(null, zoom, tilt, bearing), or calling CameraPosition.builder() / CameraPosition.Builder constructor paths where target is never set before build(), or passing a method result (e.g. a map lookup or getLocation()) that returned null directly as the target argument.

Common situations: Developers compute the target from a nullable source such as a Location object that is null before the first GPS fix, deserialize a camera state from prefs/JSON where the key is absent, or build a camera from a marker that hasn't been created yet. Restoring saved camera state across process death is a frequent source.

Related errors


AI-assisted analysis of microg/GmsCore@157c9d86ac (2026-09-06). Data as JSON: /api/errors/b78df8d5eb593e3a. Report an issue: GitHub.