{"record":{"id":"cf415538b94f8615","repo":"tui-cs/Terminal.Gui","slug":"init-must-be-called-before-run","errorCode":null,"errorMessage":"Init must be called before Run.","messagePattern":"Init must be called before Run\\.","errorType":"exception","errorClass":"NotInitializedException","httpStatus":null,"severity":"critical","filePath":"Terminal.Gui/App/ApplicationImpl.Run.cs","lineNumber":237,"sourceCode":"        Run (runnable, errorHandler);\n\n        // We created the runnable, so dispose it if it's disposable\n        if (runnable is IDisposable disposable)\n        {\n            disposable.Dispose ();\n        }\n\n        return this;\n    }\n\n    /// <inheritdoc/>\n    public object? Run (IRunnable runnable, Func<Exception, bool>? errorHandler = null)\n    {\n        ArgumentNullException.ThrowIfNull (runnable);\n\n        if (!Initialized)\n        {\n            throw new NotInitializedException (@\"Init must be called before Run.\");\n        }\n\n        // Begin the session (adds to stack, raises IsRunningChanging/IsRunningChanged)\n\n        SessionToken? token =\n\n            // Find it on the stack\n            runnable.IsRunning ? SessionStack?.FirstOrDefault (st => st.Runnable == runnable) : Begin (runnable);\n\n        if (token is null)\n        {\n            Logging.Warning (@\"Run - Begin session failed or was cancelled.\");\n\n            return null;\n        }\n\n        // Loop to handle the case where End is cancelled by an IsRunningChanging handler.\n        // When End is cancelled, IsRunning remains true; we reset StopRequested and re-run the loop.","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/App/ApplicationImpl.Run.cs#L219-L255","documentation":"Thrown as NotInitializedException by ApplicationImpl.Run(IRunnable, ...) when Initialized is false. Unlike the generic Run<T> overload which auto-initializes, this overload requires that Init has already been called because it does not take a driverName and cannot auto-init. The message is 'Init must be called before Run.'","triggerScenarios":"Calling app.Run(someRunnable) on an IApplication that was created but never had Init() called; using the explicit IRunnable overload directly after Application.Create() without Init; test code that skips initialization.","commonSituations":"Calling the wrong Run overload (the IRunnable one instead of Run<T> which auto-inits); assuming Create() also initializes; refactoring that separated Create and Init but left a Run call before Init.","solutions":["Call Init before Run: 'var app = Application.Create().Init();' (chained) before invoking Run.","Use the generic Run<T>() overload which auto-initializes if not already initialized.","Guard with 'if (!app.Initialized) app.Init();' before the Run call."],"exampleFix":"// before\nvar app = Application.Create ();\napp.Run (myRunnable); // throws — not initialized\n\n// after\nvar app = Application.Create ().Init ();\napp.Run (myRunnable);","handlingStrategy":"validation","validationCode":"if (!app.Initialized)\n{\n    app.Init ();\n}\napp.Run (runnable);","typeGuard":"static bool IsReadyToRun (IApplication app) => app.Initialized;","tryCatchPattern":null,"preventionTips":["Chain Init: var app = Application.Create().Init(); before Run.","Use Run<T>() which auto-initializes if needed.","Never call the explicit IRunnable Run overload on an uninitialized app."],"tags":["lifecycle","initialization","runnable"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}