{"record":{"id":"cd989288ec83053e","repo":"tui-cs/Terminal.Gui","slug":"file","errorCode":null,"errorMessage":"file","messagePattern":"file","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Views/TextInput/TextModel.cs","lineNumber":174,"sourceCode":"                _cachedMaxWidthPerLine [i] = maxLength;\n            }\n        }\n\n        // Cache the result when scanning the full range\n        if (first == 0 && last >= _lines.Count)\n        {\n            _cachedMaxWidth = maxLength;\n            _cachedMaxWidthTabWidth = tabWidth;\n        }\n\n        return maxLength;\n    }\n\n    public event EventHandler? LinesLoaded;\n\n    public bool LoadFile (string file)\n    {\n        FilePath = file ?? throw new ArgumentNullException (nameof (file));\n\n        using FileStream stream = File.OpenRead (file);\n\n        LoadStream (stream);\n\n        return true;\n    }\n\n    public void LoadListCells (List<List<Cell>> cellsList, Attribute? attribute)\n    {\n        _lines = cellsList;\n        SetAttributes (attribute);\n        InvalidateMaxWidthCache ();\n        OnLinesLoaded ();\n    }\n\n    public void LoadCells (List<Cell> cells, Attribute? attribute)\n    {","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Views/TextInput/TextModel.cs#L156-L192","documentation":"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.)","triggerScenarios":"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.","commonSituations":"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.","solutions":["Null-check before loading: if (path is not null) textModel.LoadFile(path);","Treat a cancelled file dialog (null result) as a no-op rather than forwarding to LoadFile.","Validate/filter recent-file entries to drop nulls before attempting to load."],"exampleFix":"// before\nstring? path = dialog.FilePath; // null when cancelled\ntextModel.LoadFile(path); // throws 176\n\n// after\nif (dialog.FilePath is { } path)\n{\n    textModel.LoadFile(path);\n}","handlingStrategy":"validation","validationCode":"if (path is not null)\n{\n    textModel.LoadFile(path);\n}","typeGuard":"static bool IsLoadablePath(string? p) => p is not null;","tryCatchPattern":null,"preventionTips":["Treat a null/cancelled dialog result as a no-op.","Filter nulls from recent-file lists before loading.","Null-check path variables from external sources before calling LoadFile."],"tags":["textmodel","file","nullable","precondition"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}