{"record":{"id":"9bc44c2bc0ce09ab","repo":"microg/GmsCore","slug":"mapvalue-is-not-a-float","errorCode":null,"errorMessage":"MapValue is not a float","messagePattern":"MapValue is not a float","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"play-services-fitness/src/main/java/com/google/android/gms/fitness/data/MapValue.java","lineNumber":45,"sourceCode":"        this.format = format;\n        this.value = value;\n    }\n\n    @NonNull\n    public static MapValue ofFloat(float value) {\n        return new MapValue(FORMAT_FLOAT, value);\n    }\n\n    public int getFormat() {\n        return format;\n    }\n\n    float getValue() {\n        return value;\n    }\n\n    public float asFloat() {\n        if (format != FORMAT_FLOAT) throw new IllegalStateException(\"MapValue is not a float\");\n        return value;\n    }\n\n    @Override\n    public int hashCode() {\n        return (int) value;\n    }\n\n    @Override\n    public void writeToParcel(@NonNull Parcel dest, int flags) {\n        CREATOR.writeToParcel(this, dest, flags);\n    }\n\n    public static final SafeParcelableCreatorAndWriter<MapValue> CREATOR = findCreator(MapValue.class);\n}\n","sourceCodeStart":27,"sourceCodeEnd":61,"githubUrl":"https://github.com/microg/GmsCore/blob/157c9d86ac46c195a86c2f15ab55c84036223f95/play-services-fitness/src/main/java/com/google/android/gms/fitness/data/MapValue.java#L27-L61","documentation":"Thrown by MapValue.asFloat() when the value's format is not FORMAT_FLOAT. MapValue wraps int or float values in fitness data; a given field holds exactly one format, so calling asFloat on an integer-formatted (or otherwise non-float) value is an invalid read of the wrapped data.","triggerScenarios":"Calling mapValue.asFloat() on a value obtained from a field whose format is FORMAT_INT (or another non-float format) — e.g. reading Field.FIELD_STEPS' value and calling asFloat instead of asInt.","commonSituations":"Copy-pasted value-reading code using asFloat for every field; fields whose format differs across data types (steps are ints, calories are floats); code written for one DataType reused with another whose fields are integer-valued.","solutions":["Call asInt() for FORMAT_INT values instead of asFloat().","Check mapValue.getFormat() == Field.FORMAT_FLOAT before calling asFloat().","Read values via dataPoint.getValue(field) and branch on getFormat().","Consult the DataType documentation to know each field's expected format."],"exampleFix":"// before\nint steps = dataPoint.getValue(Field.FIELD_STEPS).asFloat(); // wrong\n// after\nMapValue v = dataPoint.getValue(Field.FIELD_STEPS);\nint steps = (v.getFormat() == Field.FORMAT_INT) ? v.asInt() : (int) v.asFloat();","handlingStrategy":"type-guard","validationCode":"if (mapValue.getFormat() != Field.FORMAT_FLOAT) { /* use asInt() or skip */ }","typeGuard":"Float asFloatIfFloat(MapValue v) { return v.getFormat() == Field.FORMAT_FLOAT ? v.asFloat() : null; }","tryCatchPattern":"try { f = mapValue.asFloat(); } catch (IllegalStateException e) { f = (float) mapValue.asInt(); }","preventionTips":["Branch on getFormat() before reading MapValue values.","Use asInt() for integer fields like FIELD_STEPS.","Consult the DataType field table for expected formats.","Centralize value reading in a helper that handles both formats."],"tags":["android","google-fit","type-mismatch","format"],"backgroundTag":"type-mismatch","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"}