{"record":{"id":"b78df8d5eb593e3a","repo":"microg/GmsCore","slug":"null-camera-target","errorCode":null,"errorMessage":"null camera target","messagePattern":"null camera target","errorType":"validation","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"play-services-maps/src/main/java/com/google/android/gms/maps/model/CameraPosition.java","lineNumber":84,"sourceCode":"\n    /**\n     * Constructs a CameraPosition.\n     *\n     * @param target  The target location to align with the center of the screen.\n     * @param zoom    Zoom level at target. See {@link #zoom} for details of restrictions.\n     * @param tilt    The camera angle, in degrees, from the nadir (directly down). See\n     *                {@link #tilt} for details of restrictions.\n     * @param bearing Direction that the camera is pointing in, in degrees clockwise from north.\n     *                This value will be normalized to be within 0 degrees inclusive and 360\n     *                degrees exclusive.\n     * @throws NullPointerException     if {@code target} is {@code null}\n     * @throws IllegalArgumentException if {@code tilt} is outside range of {@code 0} to {@code 90}\n     *                                  degrees inclusive\n     */\n    public CameraPosition(LatLng target, float zoom, float tilt, float bearing)\n            throws NullPointerException, IllegalArgumentException {\n        if (target == null) {\n            throw new NullPointerException(\"null camera target\");\n        }\n        this.target = target;\n        this.zoom = zoom;\n        if (tilt < 0 || 90 < tilt) {\n            throw new IllegalArgumentException(\"Tilt needs to be between 0 and 90 inclusive\");\n        }\n        this.tilt = tilt;\n        if (bearing <= 0) {\n            bearing += 360;\n        }\n        this.bearing = bearing % 360;\n    }\n\n    /**\n     * Creates a builder for a camera position.\n     */\n    public static Builder builder() {\n        return new Builder();","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/microg/GmsCore/blob/157c9d86ac46c195a86c2f15ab55c84036223f95/play-services-maps/src/main/java/com/google/android/gms/maps/model/CameraPosition.java#L66-L102","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the target LatLng for null before constructing the CameraPosition and use a sensible default position when it is null.","If using CameraPosition.Builder, always call target(...) before build(); otherwise use the default CameraPosition directly.","When deserializing saved camera state, validate all fields exist and are non-null before reconstructing the position.","Wrap construction in try-catch as a last resort and fall back to a default camera position."],"exampleFix":"// before\ncameraUpdate = CameraUpdateFactory.newCameraPosition(\n    new CameraPosition(myLocation, 15f, 45f, 0f));\n\n// after\nif (myLocation != null) {\n    cameraUpdate = CameraUpdateFactory.newCameraPosition(\n        new CameraPosition(myLocation, 15f, 45f, 0f));\n} else {\n    cameraUpdate = CameraUpdateFactory.newCameraPosition(\n        new CameraPosition(DEFAULT_TARGET, 15f, 45f, 0f));\n}","handlingStrategy":"type-guard","validationCode":"if (target == null) { target = DEFAULT_TARGET; }","typeGuard":"boolean isValidTarget(LatLng t) { return t != null; }","tryCatchPattern":"try {\n    CameraPosition pos = new CameraPosition(target, zoom, tilt, bearing);\n} catch (NullPointerException e) {\n    camera.moveCamera(CameraUpdateFactory.newCameraPosition(DEFAULT_POSITION));\n}","preventionTips":["Never pass nullable location fixes or lookups directly into CameraPosition; default them first.","Use CameraPosition.Builder and always call target(...) explicitly.","Validate deserialized camera state before reconstruction.","Centralize camera construction in one helper that guarantees a non-null target."],"tags":["maps","android","null-check","camera"],"backgroundTag":"null-argument","analyzedSha":"157c9d86ac46c195a86c2f15ab55c84036223f95","analyzedAt":"2026-09-06T17:27:33.892Z","contentChangedAt":"2026-09-06T17:27:33.892Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}