microg/GmsCore · error · NullPointerException

geofence can't be null.

Error message

geofence can't be null.

What it means

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.

Source

Thrown at play-services-location/src/main/java/com/google/android/gms/location/GeofencingRequest.java:104

    /**
     * A builder that builds {@link GeofencingRequest}.
     */
    public static class Builder {
        private List<Geofence> geofences = new ArrayList<>();
        private @InitialTrigger int initialTrigger = INITIAL_TRIGGER_ENTER | INITIAL_TRIGGER_DWELL;

        /**
         * Adds a geofence to be monitored by geofencing service.
         *
         * @param geofence the geofence to be monitored. The geofence must be built with {@link Geofence.Builder}.
         * @return the builder object itself for method chaining
         * @throws IllegalArgumentException if the geofence is not built with {@link Geofence.Builder}.
         * @throws NullPointerException     if the given geofence is null
         */
        @NonNull
        public GeofencingRequest.Builder addGeofence(Geofence geofence) {
            if (geofence == null) throw new NullPointerException("geofence can't be null.");
            if (!(geofence instanceof ParcelableGeofence)) throw new IllegalArgumentException("Geofence must be created using Geofence.Builder.");
            geofences.add(geofence);
            return this;
        }

        /**
         * Adds all the geofences in the given list to be monitored by geofencing service.
         *
         * @param geofences the geofences to be monitored. The geofences in the list must be built with {@link Geofence.Builder}.
         * @return the builder object itself for method chaining
         * @throws IllegalArgumentException if the geofence is not built with {@link Geofence.Builder}.
         */
        @NonNull
        public GeofencingRequest.Builder addGeofences(List<Geofence> geofences) {
            if (geofences != null) {
                for (Geofence geofence : geofences) {
                    if (geofence != null) addGeofence(geofence);
                }

View on GitHub (pinned to 157c9d86ac)

Solutions

  1. Null-check the Geofence before calling addGeofence()
  2. Fix the upstream construction so every expected fence is built (see Geofence.Builder validation errors)
  3. Filter nulls out of any collection of fences before adding them

Example fix

// before
Geofence fence = fenceCache.get(fenceId); // may be null
requestBuilder.addGeofence(fence);
// after
Geofence fence = fenceCache.get(fenceId);
if (fence != null) {
    requestBuilder.addGeofence(fence);
}
Defensive patterns

Strategy: validation

Validate before calling

if (geofence == null) { Log.w(TAG, "Skipping null geofence"); return; }
requestBuilder.addGeofence(geofence);

Type guard

static boolean isValidGeofence(Geofence g) { return g != null; }

Try / catch

try { builder.addGeofence(fence); } catch (NullPointerException e) { Log.e(TAG, "Attempted to add null geofence", e); }

Prevention

When it happens

Trigger: Calling addGeofence(null), typically when the Geofence comes from a method/map lookup that returned null.

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

Related errors


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