{"record":{"id":"250f5abdf2a1f0d5","repo":"apache/beam","slug":"cannot-call-getfromrowfunction-when-there-is-no-schema","errorCode":null,"errorMessage":"Cannot call getFromRowFunction when there is no schema","messagePattern":"Cannot call getFromRowFunction 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":341,"sourceCode":"  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();\n  }\n\n  /**\n   * of the {@link PTransform}.\n   *\n   * @return the output of the applied {@link PTransform}\n   */\n  public <OutputT extends POutput> OutputT apply(PTransform<? super PCollection<T>, OutputT> t) {\n    return Pipeline.applyTransform(this, t);\n  }\n\n  /**\n   * Applies the given {@link PTransform} to this input {@link PCollection}, using {@code name} to\n   * identify this specific application of the transform. This name is used in various places,\n   * including the monitoring UI, logging, and to stably identify this application node in the job\n   * graph.","sourceCodeStart":323,"sourceCodeEnd":359,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollection.java#L323-L359","documentation":"PCollection.getFromRowFunction() throws this IllegalStateException when the PCollection lacks a schema. The Row -> T function only exists when the coder is a SchemaCoder; the method fails fast instead of returning null.","triggerScenarios":"Calling getFromRowFunction() directly or via fromRowFunction() on a PCollection whose coder is not a SchemaCoder, e.g. before any setSchema call or on output of non-schema transforms.","commonSituations":"Decoding Rows back into typed objects in a DoFn without a registered schema; mixing Row-based IO connectors with plain-typed PCollections.","solutions":["Guard with pc.hasSchema() before the call","Attach a schema via pc.setSchema(schema, toRowFn, fromRowFn) so the fromRowFunction is available","Register the element type with Beam's schema framework (annotated POJO/bean) so the coder is a SchemaCoder"],"exampleFix":"// before\nSerializableFunction<Row, T> fn = pc.getFromRowFunction(); // throws\n// after\nif (!pc.hasSchema()) {\n  pc = pc.setRowSchema(mySchema);\n}\nSerializableFunction<Row, T> fn = pc.getFromRowFunction();","handlingStrategy":"validation","validationCode":"if (!pc.hasSchema()) { pc = pc.setRowSchema(mySchema); }","typeGuard":"boolean canDecodeFromRow(PCollection<?> pc) { return pc.hasSchema(); }","tryCatchPattern":"try { var fn = pc.getFromRowFunction(); } catch (IllegalStateException e) { pc = pc.setRowSchema(mySchema); }","preventionTips":["Set schema with both toRow and fromRow functions","Use schema-registered POJOs so Beam derives both functions","Check hasSchema() in utility code before Row decoding"],"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"}