{"record":{"id":"5dba9c8d935b5390","repo":"apache/flink","slug":"the-runtime-context-has-not-been-initialized-yet-5dba9c","errorCode":null,"errorMessage":"The runtime context has not been initialized yet. Try accessing it in one of the other life cycle methods.","messagePattern":"The runtime context has not been initialized yet\\. Try accessing it in one of the other life cycle methods\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/io/RichOutputFormat.java","lineNumber":47,"sourceCode":"public abstract class RichOutputFormat<IT> implements OutputFormat<IT> {\n\n    private static final long serialVersionUID = 1L;\n\n    // --------------------------------------------------------------------------------------------\n    //  Runtime context access\n    // --------------------------------------------------------------------------------------------\n\n    private transient RuntimeContext runtimeContext;\n\n    public void setRuntimeContext(RuntimeContext t) {\n        this.runtimeContext = t;\n    }\n\n    public RuntimeContext getRuntimeContext() {\n        if (this.runtimeContext != null) {\n            return this.runtimeContext;\n        } else {\n            throw new IllegalStateException(\n                    \"The runtime context has not been initialized yet. Try accessing \"\n                            + \"it in one of the other life cycle methods.\");\n        }\n    }\n}\n","sourceCodeStart":29,"sourceCodeEnd":53,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/io/RichOutputFormat.java#L29-L53","documentation":"Thrown by RichOutputFormat.getRuntimeContext() when the internal runtimeContext field is still null. The Flink runtime injects the RuntimeContext by calling setRuntimeContext() before invoking life-cycle methods such as configure() and open(). Accessing it before the framework has wired it — for example in the constructor or a static initializer — triggers this IllegalStateException.","triggerScenarios":"Calling getRuntimeContext() inside a RichOutputFormat subclass constructor, a field initializer, or any method invoked before the framework calls setRuntimeContext(). Also triggered if setRuntimeContext(null) is explicitly called or if a custom OutputFormat bypasses the normal framework wiring.","commonSituations":"A developer instantiates a RichOutputFormat manually (e.g., in a unit test without the Flink test harness) and immediately tries to read metrics or broadcast variables. Or the context is accessed in a field initializer that runs at construction time rather than at open() time.","solutions":["Move the getRuntimeContext() call from the constructor or field initializer into the open() or configure() method, which are invoked after setRuntimeContext().","If writing a unit test, use ExecutionEnvironment.createLocalEnvironment() with a CollectionExecutor or use TestRuntimeContext to inject a mock context.","Check for null before calling: guard with if (getRuntimeContext() != null) or catch IllegalStateException during early init phases."],"exampleFix":"// before — in constructor\npublic MyOutputFormat() {\n    this.metricGroup = getRuntimeContext().getMetricGroup();\n}\n// after — moved to open()\npublic MyOutputFormat() {\n    // no context access here\n}\n@Override\npublic void open(int taskNumber, int numTasks) {\n    this.metricGroup = getRuntimeContext().getMetricGroup();\n}","handlingStrategy":"validation","validationCode":"// Check if RuntimeContext is available before accessing\n// Only call getRuntimeContext() inside open()/configure(), not in constructors.\npublic class MyFormat extends RichOutputFormat<IN> {\n    private RuntimeContext ctx;\n    @Override\n    public void open(int taskNumber, int numTasks) {\n        this.ctx = getRuntimeContext(); // safe: framework has set it by now\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call getRuntimeContext() in a constructor or field initializer of a RichOutputFormat.","Restrict RuntimeContext access to open() or configure() — these are called after setRuntimeContext().","In unit tests, inject a TestRuntimeContext or use createLocalEnvironment() instead of manual instantiation."],"tags":["lifecycle","runtime-context","output-format","flink-core"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}