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

  1. Ensure the host View belongs to a running IApplication: create/init the app and add the view before calling Prompt.
  2. Pass a host that is a View with a non-null App — e.g. a view already in the session stack.
  3. 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

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


AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13). Data as JSON: /api/errors/de1ec73eb82689d2. Report an issue: GitHub.