{"record":{"id":"4252bb83515c5759","repo":"microg/GmsCore","slug":"you-must-not-call-build-before-adding-points-to","errorCode":null,"errorMessage":"You must not call build() before adding points to the Builder","messagePattern":"You must not call build\\(\\) before adding points to the Builder","errorType":"validation","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"play-services-maps/src/main/java/com/google/android/gms/maps/model/LatLngBounds.java","lineNumber":200,"sourceCode":"\n    /**\n     * This is a builder that is able to create a minimum bound based on a set of LatLng points.\n     */\n    public static final class Builder {\n        private LatLngBounds bounds;\n\n        public Builder() {\n\n        }\n\n        /**\n         * Creates the LatLng bounds.\n         *\n         * @throws IllegalStateException if no points have been included.\n         */\n        public LatLngBounds build() throws IllegalStateException {\n            if (bounds == null)\n                throw new IllegalStateException(\n                        \"You must not call build() before adding points to the Builder\");\n            return bounds;\n        }\n\n        /**\n         * Includes this point for building of the bounds. The bounds will be extended in a\n         * minimum way to include this point.\n         * <p/>\n         * More precisely, it will consider extending the bounds both in the eastward and westward\n         * directions (one of which may cross the antimeridian) and choose the smaller of the two.\n         * In the case that both directions result in a LatLngBounds of the same size, this will\n         * extend it in the eastward direction. For example, adding points (0, -179) and (1, 179)\n         * will create a bound crossing the 180 longitude.\n         *\n         * @param point A {@link LatLng} to be included in the bounds.\n         * @return This builder object with a new point added.\n         */\n        public Builder include(LatLng point) {","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/microg/GmsCore/blob/157c9d86ac46c195a86c2f15ab55c84036223f95/play-services-maps/src/main/java/com/google/android/gms/maps/model/LatLngBounds.java#L182-L218","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the source list is non-empty before building: if (!points.isEmpty()) { ... } and skip the camera move otherwise.","Ensure every code path that should contribute points actually calls include() (log/verify collection contents).","Provide a fallback: when no points exist, use a default bounds or skip CameraUpdateFactory.newLatLngBounds().","Catch IllegalStateException around build() and handle the empty case gracefully."],"exampleFix":"// before\nLatLngBounds.Builder b = new LatLngBounds.Builder();\nfor (Marker m : filteredMarkers) b.include(m.getPosition());\nmap.moveCamera(CameraUpdateFactory.newLatLngBounds(b.build(), 100));\n\n// after\nif (!filteredMarkers.isEmpty()) {\n    LatLngBounds.Builder b = new LatLngBounds.Builder();\n    for (Marker m : filteredMarkers) b.include(m.getPosition());\n    map.moveCamera(CameraUpdateFactory.newLatLngBounds(b.build(), 100));\n}","handlingStrategy":"validation","validationCode":"if (points.isEmpty()) { /* skip camera fit or use default bounds */ }","typeGuard":"boolean canBuildBounds(java.util.List<LatLng> pts) { return pts != null && !pts.isEmpty(); }","tryCatchPattern":"try {\n    LatLngBounds b = builder.build();\n    map.moveCamera(CameraUpdateFactory.newLatLngBounds(b, padding));\n} catch (IllegalStateException e) {\n    // no points were added; skip the camera fit\n}","preventionTips":["Guard build() with a non-empty check on the source collection.","Handle empty filtered/search results before fitting the camera.","Verify all intended include() calls execute (log collection size in debug builds)."],"tags":["maps","android","latlng","builder","empty-collection"],"backgroundTag":"invalid-state-transition","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"}