{"record":{"id":"de1ec73eb82689d2","repo":"tui-cs/Terminal.Gui","slug":"host-runnable-must-have-an-associated-iapplication","errorCode":null,"errorMessage":"Host runnable must have an associated IApplication.","messagePattern":"Host runnable must have an associated IApplication\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Views/PromptExtensions.cs","lineNumber":103,"sourceCode":"    /// \n    ///     if (date is { } selectedDate)\n    ///     {\n    ///         MessageBox.Query (\"Date Selected\", $\"You selected: {selectedDate:yyyy-MM-dd}\", Strings.btnOk);\n    ///     }\n    ///     </code>\n    /// </example>\n    public static TResult? Prompt<TView, TResult> (\n        this IRunnable host,\n        TView? view = null,\n        Func<TView, TResult?>? resultExtractor = null,\n        TResult? input = default,\n        Action<Prompt<TView, TResult>>? beginInitHandler = null\n    )\n        where TView : View, new()\n    {\n        ArgumentNullException.ThrowIfNull (host);\n\n        IApplication app = (host as View)?.App ?? throw new InvalidOperationException (\"Host runnable must have an associated IApplication.\");\n\n        using Prompt<TView, TResult> prompt = new (view);\n\n        if (resultExtractor is { })\n        {\n            prompt.ResultExtractor = resultExtractor;\n        }\n\n        if (prompt.GetWrappedView () is IValue<TResult> iValue)\n        {\n            iValue.Value = input;\n        }\n\n        // prompt.ResultExtractor = view1 => view.Text;\n        // TODO: We need to add a InitBegun event to View that is raised by BeginInit\n        // TODO: but for now Initialized will work, but is suboptimal.\n        prompt.Initialized += (_, _) =>\n                              {","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Views/PromptExtensions.cs#L85-L121","documentation":"Thrown by the PromptExtensions.Prompt extension method when the host IRunnable is not a View, or is a View whose App is null. The method needs an IApplication to construct and run the Prompt, so it casts host to View and reads .App; if either the cast or the App property yields null, it cannot proceed. This indicates the host was never added to an initialised application tree.","triggerScenarios":"Calling host.Prompt(...) on an IRunnable that is not a View, or on a View that has not been added to an initialised IApplication (App still null). Also when calling Prompt before Application.Create().Init() / before running a root.","commonSituations":"Unit tests that construct a Runnable in isolation without an app; calling Prompt on a freshly new'd View that was never added to the view hierarchy; ordering bug where Prompt is invoked during construction instead of after Run begins.","solutions":["Ensure the host View belongs to a running IApplication: create/init the app and add the view before calling Prompt.","Pass a host that is a View with a non-null App — e.g. a view already in the session stack.","In tests, stand up an IApplication (Application.Create().Init()) and route through it so App is populated."],"exampleFix":"// before\nvar dlg = new MyDialog();\ndlg.Prompt(); // App is null -> throws 169\n\n// after\nIApplication app = Application.Create().Init();\napp.Run<MyWindow>(() => myHostView.Prompt());","handlingStrategy":"validation","validationCode":"if (host is View { App: IApplication app })\n{\n    host.Prompt(...);\n}\nelse\n{\n    // initialise the application first\n}","typeGuard":"static bool HostIsReady(IRunnable host) => host is View { App: not null };","tryCatchPattern":null,"preventionTips":["Create and init an IApplication before calling Prompt.","Call Prompt from a view already in the running session stack.","In tests, stand up Application.Create().Init() first."],"tags":["prompt","runnable","lifecycle","precondition"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}