{"record":{"id":"9e3e557e8539bb98","repo":"microg/GmsCore","slug":"dataset-has-already-been-built","errorCode":null,"errorMessage":"DataSet has already been built.","messagePattern":"DataSet has already been built\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"play-services-fitness/src/main/java/com/google/android/gms/fitness/data/DataSet.java","lineNumber":178,"sourceCode":"     * Builder used to create new data sets.\n     */\n    public static class Builder {\n        private final DataSet dataSet;\n        private boolean built = false;\n\n        Builder(DataSource dataSource) {\n            this.dataSet = DataSet.create(dataSource);\n        }\n\n        /**\n         * Adds a data point to this data set. The data points should be for the correct data type and data source, and should have its timestamp\n         * already set.\n         *\n         * @throws IllegalArgumentException If dataPoint has the wrong {@link DataSource}, or contain invalid data.\n         */\n        @NonNull\n        public Builder add(@NonNull DataPoint dataPoint) {\n            if (built) throw new IllegalStateException(\"DataSet has already been built.\");\n            this.dataSet.add(dataPoint);\n            return this;\n        }\n\n        /**\n         * Adds a list of data points to this data set in bulk. All data points should be for the correct data type and data source, and should have their\n         * timestamp already set.\n         *\n         * @throws IllegalArgumentException If the {@code dataPoints} have the wrong source, or contain invalid data.\n         */\n        @NonNull\n        public Builder addAll(@NonNull Iterable<DataPoint> iterable) {\n            if (built) throw new IllegalStateException(\"DataSet has already been built.\");\n            this.dataSet.addAll(iterable);\n            return this;\n        }\n\n        /**","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/microg/GmsCore/blob/157c9d86ac46c195a86c2f15ab55c84036223f95/play-services-fitness/src/main/java/com/google/android/gms/fitness/data/DataSet.java#L160-L196","documentation":"Thrown by DataSet.Builder.add(DataPoint) after build() has already been called. The DataSet.Builder is single-use; once the DataSet is built, adding more points is rejected as an invalid state to prevent mutating an immutable, possibly already-consumed DataSet.","triggerScenarios":"Holding a DataSet.Builder reference, calling build(), then calling Builder.add(dataPoint) with a new point — often when streaming additional points after an initial build.","commonSituations":"Incremental data collection where code keeps appending to a builder after committing the DataSet; sharing a Builder across threads or callbacks where one path builds and another keeps adding.","solutions":["Call add() for ALL data points before invoking build().","Create a new DataSet.builder(dataSource) for each batch of points you want to commit separately.","Restructure collection logic to accumulate points first, then build once at the end.","Never cache or share a Builder instance after build() — treat it as consumed."],"exampleFix":"// before\nDataSet.Builder b = DataSet.builder(ds);\nb.build();\nb.add(laterPoint); // throws\n// after\nDataSet first = DataSet.builder(ds).add(p1).build();\nDataSet second = DataSet.builder(ds).add(laterPoint).build();","handlingStrategy":"validation","validationCode":"if (builderConsumed) { builder = DataSet.builder(dataSource); }","typeGuard":"boolean canAdd(DataSet.Builder b) { return !b.isBuilt(); } // track in your own wrapper","tryCatchPattern":"try { builder.add(point); } catch (IllegalStateException e) { builder = DataSet.builder(dataSource); builder.add(point); }","preventionTips":["Call build() only after all points are added.","Create a fresh builder per batch/commit.","Discard the Builder reference right after build().","Avoid sharing builders across threads or callbacks."],"tags":["android","google-fit","builder-misuse","illegal-state"],"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-14T05:17:10.506Z"}