{"record":{"id":"6eaaa28b38d2d775","repo":"apache/beam","slug":"cannot-call-getschema-when-there-is-no-schema","errorCode":null,"errorMessage":"Cannot call getSchema when there is no schema","messagePattern":"Cannot call getSchema when there is no schema","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollection.java","lineNumber":325,"sourceCode":"\n  /** Sets a {@link Schema} on this {@link PCollection}. */\n  public PCollection<T> setSchema(\n      Schema schema,\n      TypeDescriptor<T> typeDescriptor,\n      SerializableFunction<T, Row> toRowFunction,\n      SerializableFunction<Row, T> fromRowFunction) {\n    return setCoder(SchemaCoder.of(schema, typeDescriptor, toRowFunction, fromRowFunction));\n  }\n\n  /** Returns whether this {@link PCollection} has an attached schema. */\n  public boolean hasSchema() {\n    return coderOrFailure.coder != null && coderOrFailure.coder instanceof SchemaCoder;\n  }\n\n  /** Returns the attached schema. */\n  public Schema getSchema() {\n    if (!hasSchema()) {\n      throw new IllegalStateException(\"Cannot call getSchema when there is no schema\");\n    }\n    return ((SchemaCoder) getCoder()).getSchema();\n  }\n\n  /** Returns the attached schema's toRowFunction. */\n  public SerializableFunction<T, Row> getToRowFunction() {\n    if (!hasSchema()) {\n      throw new IllegalStateException(\"Cannot call getToRowFunction when there is no schema\");\n    }\n    return ((SchemaCoder<T>) getCoder()).getToRowFunction();\n  }\n\n  /** Returns the attached schema's fromRowFunction. */\n  public SerializableFunction<Row, T> getFromRowFunction() {\n    if (!hasSchema()) {\n      throw new IllegalStateException(\"Cannot call getFromRowFunction when there is no schema\");\n    }\n    return ((SchemaCoder<T>) getCoder()).getFromRowFunction();","sourceCodeStart":307,"sourceCodeEnd":343,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollection.java#L307-L343","documentation":"This IllegalStateException is thrown by PCollection.getSchema() when the PCollection has no schema attached. In Beam, schemas are only present if the coder is a SchemaCoder (set via setSchema or schema-aware transforms); calling getSchema on a plain-coded PCollection is an invalid state, so the library fails fast instead of returning null.","triggerScenarios":"Calling getSchema(), or any caller that delegates to it (schema(), converted(), keySchema(), inputSchema(), addFieldsInformation()), on a PCollection whose coder is not a SchemaCoder — e.g. a PCollection created from Create.of with simple types, or output of a transform that did not call setSchema.","commonSituations":"Expecting a schema after a ParDo/MapElements that produces plain types; using schema helpers (rows(), recordClass()) on collections from non-schema-aware sources; forgetting to apply FieldAccessDescriptor/schema registration before schema-based operations.","solutions":["Verify with pc.hasSchema() before calling getSchema()","Attach a schema via pc.setSchema(schema, toRowFn, fromRowFn) or use schema-aware transforms like setRowSchema/TypedSchemaTransform output","Convert the element type to a schema-registered class (Java beans, @DefaultSchema annotated types) so the coder becomes a SchemaCoder","Use getCoder() and check instanceof SchemaCoder to access the schema manually"],"exampleFix":"// before\nSchema schema = pc.getSchema(); // IllegalStateException\n// after\nif (pc.hasSchema()) {\n  Schema schema = pc.getSchema();\n} else {\n  pc = pc.setRowSchema(MyBean.class.getDeclaredSchema());\n}","handlingStrategy":"validation","validationCode":"if (!pc.hasSchema()) { throw new IllegalStateException(\"PCollection has no schema; call setSchema first\"); }","typeGuard":"boolean hasSchema(PCollection<?> pc) { return pc.hasSchema(); }","tryCatchPattern":"try { Schema s = pc.getSchema(); } catch (IllegalStateException e) { /* attach schema or fallback */ }","preventionTips":["Always call hasSchema() before schema accessors","Prefer schema-aware transforms that guarantee a SchemaCoder","Register element types with @DefaultSchema annotations","Keep a shared helper that attaches the schema once at pipeline construction"],"tags":["java","apache-beam","schema","illegal-state"],"backgroundTag":"missing-required-config","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}