{"record":{"id":"d23cf61764140a29","repo":"microg/GmsCore","slug":"geofence-can-t-be-null","errorCode":null,"errorMessage":"geofence can't be null.","messagePattern":"geofence can't be null\\.","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"play-services-location/src/main/java/com/google/android/gms/location/GeofencingRequest.java","lineNumber":104,"sourceCode":"\n    /**\n     * A builder that builds {@link GeofencingRequest}.\n     */\n    public static class Builder {\n        private List<Geofence> geofences = new ArrayList<>();\n        private @InitialTrigger int initialTrigger = INITIAL_TRIGGER_ENTER | INITIAL_TRIGGER_DWELL;\n\n        /**\n         * Adds a geofence to be monitored by geofencing service.\n         *\n         * @param geofence the geofence to be monitored. The geofence must be built with {@link Geofence.Builder}.\n         * @return the builder object itself for method chaining\n         * @throws IllegalArgumentException if the geofence is not built with {@link Geofence.Builder}.\n         * @throws NullPointerException     if the given geofence is null\n         */\n        @NonNull\n        public GeofencingRequest.Builder addGeofence(Geofence geofence) {\n            if (geofence == null) throw new NullPointerException(\"geofence can't be null.\");\n            if (!(geofence instanceof ParcelableGeofence)) throw new IllegalArgumentException(\"Geofence must be created using Geofence.Builder.\");\n            geofences.add(geofence);\n            return this;\n        }\n\n        /**\n         * Adds all the geofences in the given list to be monitored by geofencing service.\n         *\n         * @param geofences the geofences to be monitored. The geofences in the list must be built with {@link Geofence.Builder}.\n         * @return the builder object itself for method chaining\n         * @throws IllegalArgumentException if the geofence is not built with {@link Geofence.Builder}.\n         */\n        @NonNull\n        public GeofencingRequest.Builder addGeofences(List<Geofence> geofences) {\n            if (geofences != null) {\n                for (Geofence geofence : geofences) {\n                    if (geofence != null) addGeofence(geofence);\n                }","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/microg/GmsCore/blob/157c9d86ac46c195a86c2f15ab55c84036223f95/play-services-location/src/main/java/com/google/android/gms/location/GeofencingRequest.java#L86-L122","documentation":"GeofencingRequest.Builder.addGeofence() rejects null geofences with a NullPointerException, since a null entry could never be monitored and would break the request payload. The Javadoc explicitly documents @throws NullPointerException for null input.","triggerScenarios":"Calling addGeofence(null), typically when the Geofence comes from a method/map lookup that returned null.","commonSituations":"Building fences from a list where one builder returned null; storing fences in a map by id and fetching a missing id; conditional fence creation that silently skipped one entry.","solutions":["Null-check the Geofence before calling addGeofence()","Fix the upstream construction so every expected fence is built (see Geofence.Builder validation errors)","Filter nulls out of any collection of fences before adding them"],"exampleFix":"// before\nGeofence fence = fenceCache.get(fenceId); // may be null\nrequestBuilder.addGeofence(fence);\n// after\nGeofence fence = fenceCache.get(fenceId);\nif (fence != null) {\n    requestBuilder.addGeofence(fence);\n}","handlingStrategy":"validation","validationCode":"if (geofence == null) { Log.w(TAG, \"Skipping null geofence\"); return; }\nrequestBuilder.addGeofence(geofence);","typeGuard":"static boolean isValidGeofence(Geofence g) { return g != null; }","tryCatchPattern":"try { builder.addGeofence(fence); } catch (NullPointerException e) { Log.e(TAG, \"Attempted to add null geofence\", e); }","preventionTips":["Null-check geofences obtained from maps/caches before adding","Filter nulls from collections: fences.stream().filter(Objects::nonNull).forEach(builder::addGeofence)"],"tags":["geofencing","null-pointer","android","validation"],"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"}