tui-cs/Terminal.Gui · error · ArgumentNullException

file

Error message

file

What it means

Thrown by TextModel.LoadFile (ArgumentNullException, param 'file') when the file argument is null. LoadFile opens a FileStream from the path and loads it, so a null path is invalid immediately, before any file access. (Note: the underlying File.OpenRead will separately throw for non-existent paths.)

Source

Thrown at Terminal.Gui/Views/TextInput/TextModel.cs:174

                _cachedMaxWidthPerLine [i] = maxLength;
            }
        }

        // Cache the result when scanning the full range
        if (first == 0 && last >= _lines.Count)
        {
            _cachedMaxWidth = maxLength;
            _cachedMaxWidthTabWidth = tabWidth;
        }

        return maxLength;
    }

    public event EventHandler? LinesLoaded;

    public bool LoadFile (string file)
    {
        FilePath = file ?? throw new ArgumentNullException (nameof (file));

        using FileStream stream = File.OpenRead (file);

        LoadStream (stream);

        return true;
    }

    public void LoadListCells (List<List<Cell>> cellsList, Attribute? attribute)
    {
        _lines = cellsList;
        SetAttributes (attribute);
        InvalidateMaxWidthCache ();
        OnLinesLoaded ();
    }

    public void LoadCells (List<Cell> cells, Attribute? attribute)
    {

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Null-check before loading: if (path is not null) textModel.LoadFile(path);
  2. Treat a cancelled file dialog (null result) as a no-op rather than forwarding to LoadFile.
  3. Validate/filter recent-file entries to drop nulls before attempting to load.

Example fix

// before
string? path = dialog.FilePath; // null when cancelled
textModel.LoadFile(path); // throws 176

// after
if (dialog.FilePath is { } path)
{
    textModel.LoadFile(path);
}
Defensive patterns

Strategy: validation

Validate before calling

if (path is not null)
{
    textModel.LoadFile(path);
}

Type guard

static bool IsLoadablePath(string? p) => p is not null;

Prevention

When it happens

Trigger: Passing a null path variable (e.g. from a dialog that returned null/cancelled), an uninitialised field, or a deserialised value that defaulted to null.

Common situations: FileDialog/OpenDialog that was cancelled returning null and the caller forwarding it unchecked; recent-file lists with a null entry; binding a TextBox to a model whose FilePath is null at load time.

Related errors


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