microg/GmsCore · error · IllegalStateException

You must not call build() before adding points to the Builde

Error message

You must not call build() before adding points to the Builder

What it means

LatLngBounds.Builder.build() throws IllegalStateException when no points were added via include() — the internal accumulated bounds is still null. Building bounds requires at least one point from which to derive the southwest/northeast corners. This is a usage-order (lifecycle) error rather than a bad-value error.

Source

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

    /**
     * This is a builder that is able to create a minimum bound based on a set of LatLng points.
     */
    public static final class Builder {
        private LatLngBounds bounds;

        public Builder() {

        }

        /**
         * Creates the LatLng bounds.
         *
         * @throws IllegalStateException if no points have been included.
         */
        public LatLngBounds build() throws IllegalStateException {
            if (bounds == null)
                throw new IllegalStateException(
                        "You must not call build() before adding points to the Builder");
            return bounds;
        }

        /**
         * Includes this point for building of the bounds. The bounds will be extended in a
         * minimum way to include this point.
         * <p/>
         * More precisely, it will consider extending the bounds both in the eastward and westward
         * directions (one of which may cross the antimeridian) and choose the smaller of the two.
         * In the case that both directions result in a LatLngBounds of the same size, this will
         * extend it in the eastward direction. For example, adding points (0, -179) and (1, 179)
         * will create a bound crossing the 180 longitude.
         *
         * @param point A {@link LatLng} to be included in the bounds.
         * @return This builder object with a new point added.
         */
        public Builder include(LatLng point) {

View on GitHub (pinned to 157c9d86ac)

Solutions

  1. Check the source list is non-empty before building: if (!points.isEmpty()) { ... } and skip the camera move otherwise.
  2. Ensure every code path that should contribute points actually calls include() (log/verify collection contents).
  3. Provide a fallback: when no points exist, use a default bounds or skip CameraUpdateFactory.newLatLngBounds().
  4. Catch IllegalStateException around build() and handle the empty case gracefully.

Example fix

// before
LatLngBounds.Builder b = new LatLngBounds.Builder();
for (Marker m : filteredMarkers) b.include(m.getPosition());
map.moveCamera(CameraUpdateFactory.newLatLngBounds(b.build(), 100));

// after
if (!filteredMarkers.isEmpty()) {
    LatLngBounds.Builder b = new LatLngBounds.Builder();
    for (Marker m : filteredMarkers) b.include(m.getPosition());
    map.moveCamera(CameraUpdateFactory.newLatLngBounds(b.build(), 100));
}
Defensive patterns

Strategy: validation

Validate before calling

if (points.isEmpty()) { /* skip camera fit or use default bounds */ }

Type guard

boolean canBuildBounds(java.util.List<LatLng> pts) { return pts != null && !pts.isEmpty(); }

Try / catch

try {
    LatLngBounds b = builder.build();
    map.moveCamera(CameraUpdateFactory.newLatLngBounds(b, padding));
} catch (IllegalStateException e) {
    // no points were added; skip the camera fit
}

Prevention

When it happens

Trigger: Calling builder.build() on a builder where include() was never called — e.g. an empty marker/point list was iterated, or a conditional include-block never executed.

Common situations: Fitting the camera to a list of markers that is empty (filtered results, before data loads); building bounds from a collection that failed to populate; guard-less generic helper that always calls build() regardless of input size.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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