tui-cs/Terminal.Gui · error · InvalidOperationException
Host runnable must have an associated IApplication.
Error message
Host runnable must have an associated IApplication.
What it means
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.
Source
Thrown at Terminal.Gui/Views/PromptExtensions.cs:103
///
/// if (date is { } selectedDate)
/// {
/// MessageBox.Query ("Date Selected", $"You selected: {selectedDate:yyyy-MM-dd}", Strings.btnOk);
/// }
/// </code>
/// </example>
public static TResult? Prompt<TView, TResult> (
this IRunnable host,
TView? view = null,
Func<TView, TResult?>? resultExtractor = null,
TResult? input = default,
Action<Prompt<TView, TResult>>? beginInitHandler = null
)
where TView : View, new()
{
ArgumentNullException.ThrowIfNull (host);
IApplication app = (host as View)?.App ?? throw new InvalidOperationException ("Host runnable must have an associated IApplication.");
using Prompt<TView, TResult> prompt = new (view);
if (resultExtractor is { })
{
prompt.ResultExtractor = resultExtractor;
}
if (prompt.GetWrappedView () is IValue<TResult> iValue)
{
iValue.Value = input;
}
// prompt.ResultExtractor = view1 => view.Text;
// TODO: We need to add a InitBegun event to View that is raised by BeginInit
// TODO: but for now Initialized will work, but is suboptimal.
prompt.Initialized += (_, _) =>
{View on GitHub (pinned to 2e47b11478)
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.
Example fix
// before var dlg = new MyDialog(); dlg.Prompt(); // App is null -> throws 169 // after IApplication app = Application.Create().Init(); app.Run<MyWindow>(() => myHostView.Prompt());
Defensive patterns
Strategy: validation
Validate before calling
if (host is View { App: IApplication app })
{
host.Prompt(...);
}
else
{
// initialise the application first
} Type guard
static bool HostIsReady(IRunnable host) => host is View { App: not null }; Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The runnable is already running.
- Attempt to Run the runnable that's already the top runnable.
- Init must be called before Run.
- Init called multiple times without Shutdown
- Cannot use legacy static Application model (Application.Init
AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13).
Data as JSON: /api/errors/de1ec73eb82689d2.
Report an issue: GitHub.