{"record":{"id":"2745dd181ae85fe8","repo":"apache/flink","slug":"the-runtime-context-has-not-been-initialized-yet","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/RichInputFormat.java","lineNumber":51,"sourceCode":"public abstract class RichInputFormat<OT, T extends InputSplit> implements InputFormat<OT, T> {\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    /**\n     * Opens this InputFormat instance. This method is called once per parallel instance. Resources\n     * should be allocated in this method. (e.g. database connections, cache, etc.)\n     *\n     * @see InputFormat\n     * @throws IOException in case allocating the resources failed.\n     */\n    @PublicEvolving\n    public void openInputFormat() throws IOException {\n        // do nothing here, just for subclasses\n    }\n\n    /**","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/io/RichInputFormat.java#L33-L69","documentation":"Thrown by RichInputFormat.getRuntimeContext() when runtimeContext is still null, i.e. setRuntimeContext has not been called yet. The framework calls setRuntimeContext during the input format's lifecycle (before open), so accessing the context too early — e.g. in the constructor or configure() — fails. The message explicitly suggests using a later lifecycle method.","triggerScenarios":"A RichInputFormat subclass calls getRuntimeContext() (directly or transitively, e.g. via getRuntimeContext().getIndexOfThisSubtask()) inside its constructor, inside configure(Configuration), or in any code that runs before the framework has injected the context.","commonSituations":"Subclass constructor that tries to read parallelism/subtask index; configure() that pre-computes per-subtask state; field initializers that call getRuntimeContext; copy-paste from open() into an earlier method.","solutions":["Move any code that needs RuntimeContext out of the constructor/configure and into open() or openInputFormat(), which run after setRuntimeContext.","If you need configuration values early, pass them via the Configuration in configure() rather than reading them from RuntimeContext.","Cache nothing context-derived in fields at construction time; compute it lazily inside open()."],"exampleFix":"// before: context accessed in constructor/configure -> throws\npublic MyFormat() { this.subtask = getRuntimeContext().getIndexOfThisSubtask(); }\n// after: access context in open()\npublic MyFormat() { }\npublic void open(InputSplit split) { this.subtask = getRuntimeContext().getIndexOfThisSubtask(); ... }","handlingStrategy":"validation","validationCode":"// Only call getRuntimeContext inside lifecycle methods that run AFTER setRuntimeContext.\n// As a guard, assert non-null before use:\nRuntimeContext ctx = getRuntimeContext(); // safe inside open()/openInputFormat()\nif (ctx == null) {\n    throw new IllegalStateException(\n        \"RuntimeContext unavailable here; move this call into open() or openInputFormat().\");\n}","typeGuard":"// Lifecycle-stage guard: defer context-dependent work until open()\npublic class MyFormat extends RichInputFormat<...> {\n    private transient boolean opened;\n    public void open(InputSplit s) { this.opened = true; init(); }\n    private void init() {\n        RuntimeContext ctx = getRuntimeContext(); // safe: open() runs after setRuntimeContext\n        ...\n    }\n}","tryCatchPattern":"try {\n    return getRuntimeContext();\n} catch (IllegalStateException e) {\n    if (e.getMessage().contains(\"not been initialized\")) {\n        throw new IllegalStateException(\n            \"getRuntimeContext called too early; move it into open() or openInputFormat().\", e);\n    }\n    throw e;\n}","preventionTips":["Never call getRuntimeContext in a constructor or configure(); only in open()/openInputFormat().","Pass early-stage configuration through the Configuration parameter, not RuntimeContext.","Avoid field initializers that depend on the context; compute lazily inside open().","Unit-test lifecycle ordering if your format depends on the context."],"tags":["input-format","runtime-context","lifecycle","api-misuse"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}