tui-cs/Terminal.Gui · critical · NotInitializedException

Init must be called before Run.

Error message

Init must be called before Run.

What it means

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.'

Source

Thrown at Terminal.Gui/App/ApplicationImpl.Run.cs:237

        Run (runnable, errorHandler);

        // We created the runnable, so dispose it if it's disposable
        if (runnable is IDisposable disposable)
        {
            disposable.Dispose ();
        }

        return this;
    }

    /// <inheritdoc/>
    public object? Run (IRunnable runnable, Func<Exception, bool>? errorHandler = null)
    {
        ArgumentNullException.ThrowIfNull (runnable);

        if (!Initialized)
        {
            throw new NotInitializedException (@"Init must be called before Run.");
        }

        // Begin the session (adds to stack, raises IsRunningChanging/IsRunningChanged)

        SessionToken? token =

            // Find it on the stack
            runnable.IsRunning ? SessionStack?.FirstOrDefault (st => st.Runnable == runnable) : Begin (runnable);

        if (token is null)
        {
            Logging.Warning (@"Run - Begin session failed or was cancelled.");

            return null;
        }

        // Loop to handle the case where End is cancelled by an IsRunningChanging handler.
        // When End is cancelled, IsRunning remains true; we reset StopRequested and re-run the loop.

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Call Init before Run: 'var app = Application.Create().Init();' (chained) before invoking Run.
  2. Use the generic Run<T>() overload which auto-initializes if not already initialized.
  3. Guard with 'if (!app.Initialized) app.Init();' before the Run call.

Example fix

// before
var app = Application.Create ();
app.Run (myRunnable); // throws — not initialized

// after
var app = Application.Create ().Init ();
app.Run (myRunnable);
Defensive patterns

Strategy: validation

Validate before calling

if (!app.Initialized)
{
    app.Init ();
}
app.Run (runnable);

Type guard

static bool IsReadyToRun (IApplication app) => app.Initialized;

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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