{"record":{"id":"4b108f7c3a779642","repo":"apache/beam","slug":"value-is-not-valid-for-displaydata-type-s-s","errorCode":null,"errorMessage":"Value is not valid for DisplayData type %s: %s","messagePattern":"Value is not valid for DisplayData type (.+?): (.+?)","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/display/DisplayData.java","lineNumber":627,"sourceCode":"    },\n    DURATION {\n      @Override\n      FormattedItemValue format(Object value) {\n        Duration duration = checkType(value, Duration.class, DURATION);\n        return new FormattedItemValue(duration.getMillis());\n      }\n    },\n    JAVA_CLASS {\n      @Override\n      FormattedItemValue format(Object value) {\n        Class<?> clazz = checkType(value, Class.class, JAVA_CLASS);\n        return new FormattedItemValue(clazz.getName(), ReflectHelpers.getSimpleName(clazz));\n      }\n    };\n\n    private static <T> T checkType(Object value, Class<T> clazz, DisplayData.Type expectedType) {\n      if (!clazz.isAssignableFrom(value.getClass())) {\n        throw new ClassCastException(\n            String.format(\"Value is not valid for DisplayData type %s: %s\", expectedType, value));\n      }\n\n      @SuppressWarnings(\"unchecked\") // type checked above.\n      T typedValue = (T) value;\n      return typedValue;\n    }\n\n    /**\n     * Format the display data value into a long string representation, and optionally a shorter\n     * representation for display.\n     *\n     * <p>Internal-only. Value objects can be safely cast to the expected Java type.\n     */\n    abstract FormattedItemValue format(Object value);\n\n    /**\n     * Safe version of {@link Type#format(Object)}, which checks for null input value and if so","sourceCodeStart":609,"sourceCodeEnd":645,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/display/DisplayData.java#L609-L645","documentation":"DisplayData.item()/named() value helpers call checkType() to assert the runtime value matches the DisplayData.Type it is being registered as (e.g. a Java class registered as DisplayData.Type.CLASS). If value.getClass() is not assignable to the expected class, a ClassCastException is thrown with this message. It indicates the wrong display-data registration method was used for the value's type.","triggerScenarios":"In a DoFn/transform's populateDisplayData(builder), calling e.g. DisplayData.instant(value) or DisplayData.duration(value) or clazz-registration with an object of the wrong runtime type — e.g. passing a String where an Instant is required, or a non-Class object where a Class is expected.","commonSituations":"Refactoring a transform's field types without updating populateDisplayData; copy-pasting display data registration lines between transforms; passing boxed types vs primitives mismatch.","solutions":["Match the registration method to the value type (item(String) for strings, instant(Instant), duration(Duration/ReadableDuration), etc.)","Convert the value before registering (e.g. value.toString() if you want it as a STRING item)","Update populateDisplayData after changing field types","Catch ClassCastException in test harnesses that exercise populateDisplayData to catch mismatches in unit tests"],"exampleFix":"// before\nbuilder.add(DisplayData.item(\"deadline\", DisplayData.Type.INSTANT, \"2024-01-01T00:00:00Z\")); // String\n// after\nbuilder.add(DisplayData.item(\"deadline\", DisplayData.Type.INSTANT, Instant.parse(\"2024-01-01T00:00:00Z\")));","handlingStrategy":"type-guard","validationCode":"if (!(value instanceof Instant)) { throw new IllegalArgumentException(\"instant() requires an Instant\"); }","typeGuard":"static <T> boolean isAssignable(Object value, Class<T> clazz) { return value != null && clazz.isAssignableFrom(value.getClass()); }","tryCatchPattern":"try { builder.add(DisplayData.item(key, DisplayData.Type.INSTANT, value)); } catch (ClassCastException e) { /* register as STRING via toString() instead */ }","preventionTips":["Match DisplayData registration helpers to exact value types","Add a unit test invoking populateDisplayData for every transform","Convert mismatched values with toString()/parsing before registration"],"tags":["beam","display-data","type-mismatch","classcast"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}