{"record":{"id":"9e172a2a02c9cf73","repo":"microg/GmsCore","slug":"type-must-be-set","errorCode":null,"errorMessage":"type must be set","messagePattern":"type must be set","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"play-services-fitness/src/main/java/com/google/android/gms/fitness/data/DataSource.java","lineNumber":200,"sourceCode":"     * A builder that can be used to construct new data source objects. In general, a built data source should be saved in memory to avoid the cost\n     * of re-constructing it for every request.\n     */\n    public static class Builder {\n        private DataType dataType;\n        private Device device;\n        private Application application;\n        private int type = -1;\n        private String streamName = \"\";\n\n        /**\n         * Finishes building the data source and returns a DataSource object.\n         *\n         * @throws IllegalStateException If the builder didn't have enough data to build a valid data source.\n         */\n        @NonNull\n        public DataSource build() {\n            if (dataType == null) throw new IllegalStateException(\"dataType must be set\");\n            if (type < 0) throw new IllegalStateException(\"type must be set\");\n            return new DataSource(dataType, type, device, application, streamName);\n        }\n\n        /**\n         * Sets the package name for the application that is recording or computing the data. Used for data sources that aren't built into the platform\n         * (local sensors and BLE sensors are built-in). It can be used to identify the data source, to disambiguate between data from different\n         * applications, and also to link back to the original application for a detailed view.\n         */\n        @NonNull\n        public Builder setAppPackageName(@NonNull String packageName) {\n            Application application = Application.GMS_APP;\n            this.application = Constants.GMS_PACKAGE_NAME.equals(packageName) ? Application.GMS_APP : new Application(packageName);\n            return this;\n        }\n\n        /**\n         * Sets the package name for the application that is recording or computing the data based on the app's context. This method should be\n         * preferred when an application is creating a data source that represents its own data. When creating a data source to query data from other","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/microg/GmsCore/blob/157c9d86ac46c195a86c2f15ab55c84036223f95/play-services-fitness/src/main/java/com/google/android/gms/fitness/data/DataSource.java#L182-L218","documentation":"Thrown by DataSource.Builder.build() when the source type (raw/derived) was never set — represented internally as type < 0. A DataSource must specify whether it is a raw sensor source or a derived/computed source, so the builder rejects construction without it.","triggerScenarios":"Calling DataSource.Builder.build() without calling setType(DataSource.TYPE_RAW) (or TYPE_DERIVED / TYPE_CLOUD, etc.). Note this is the data-source type, not the DataType, so it can be missed even when setDataType was called.","commonSituations":"Developers confusing DataType with source type and setting only setDataType; snippets from older samples predating the required setType; builders assembled programmatically where setType is conditionally skipped.","solutions":["Add .setType(DataSource.TYPE_RAW) (or TYPE_DERIVED as appropriate) before build().","Order the builder calls so setType immediately follows setDataType and neither can be skipped.","For derived/computed data use DataSource.TYPE_DERIVED; use TYPE_RAW for direct sensor data.","Validate your builder config in unit tests so missing setType fails fast in CI."],"exampleFix":"// before\nDataSource src = new DataSource.Builder()\n    .setDataType(DataType.TYPE_STEP_COUNT_DELTA)\n    .build(); // throws\n// after\nDataSource src = new DataSource.Builder()\n    .setDataType(DataType.TYPE_STEP_COUNT_DELTA)\n    .setType(DataSource.TYPE_RAW)\n    .build();","handlingStrategy":"validation","validationCode":"if (sourceType < 0) throw new IllegalStateException(\"setType must be called before build()\");","typeGuard":"boolean sourceTypeSet(int type) { return type == DataSource.TYPE_RAW || type == DataSource.TYPE_DERIVED || type == DataSource.TYPE_CLOUD; }","tryCatchPattern":"try { src = dataSourceBuilder.build(); } catch (IllegalStateException e) { throw new ConfigException(\"DataSource missing type\", e); }","preventionTips":["Always include .setType(DataSource.TYPE_RAW or TYPE_DERIVED) in the chain.","Remember source type is separate from DataType.","Use a single factory method so required calls can't be skipped.","Add a smoke test that builds all app DataSources."],"tags":["android","google-fit","missing-required-field","builder"],"backgroundTag":"missing-required-argument","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"}