{"record":{"id":"d22aed76f774a3f8","repo":"spectreconsole/spectre.console","slug":"value-cannot-be-null-parameter-writer","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'writer')","messagePattern":"Value cannot be null\\. \\(Parameter 'writer'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Spectre.Console/AnsiConsoleOutput.cs","lineNumber":42,"sourceCode":"            }\n\n            return false;\n        }\n    }\n\n    /// <inheritdoc/>\n    public int Width => ConsoleHelper.GetSafeWidth();\n\n    /// <inheritdoc/>\n    public int Height => ConsoleHelper.GetSafeHeight();\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"AnsiConsoleOutput\"/> class.\n    /// </summary>\n    /// <param name=\"writer\">The output writer.</param>\n    public AnsiConsoleOutput(TextWriter writer)\n    {\n        Writer = writer ?? throw new ArgumentNullException(nameof(writer));\n    }\n\n    /// <inheritdoc/>\n    public void SetEncoding(Encoding encoding)\n    {\n        if (Writer.IsStandardOut() || Writer.IsStandardError())\n        {\n            System.Console.OutputEncoding = encoding;\n        }\n    }\n}","sourceCodeStart":24,"sourceCodeEnd":53,"githubUrl":"https://github.com/spectreconsole/spectre.console/blob/0acc92fada6c42f13984e79c2b5f3d993bdfb099/src/Spectre.Console/AnsiConsoleOutput.cs#L24-L53","documentation":"Thrown by the AnsiConsoleOutput constructor when the supplied TextWriter is null. AnsiConsoleOutput is immutable once built, so the writer must be provided up front; a null writer would make every later write fail, so it is rejected immediately as an ArgumentNullException.","triggerScenarios":"Calling new AnsiConsoleOutput(null) or otherwise constructing it with a null TextWriter reference.","commonSituations":"Passing a writer field that was never initialized; a DI/lookup that returns null for the configured output; refactoring that drops the Console.Out argument; test helper that forgot to supply a StringWriter.","solutions":["Pass a concrete TextWriter such as Console.Out, Console.Error, or a new StringWriter()","Initialize the writer variable before constructing AnsiConsoleOutput","Use a nullable annotation / null check at the call site if the writer source may be absent"],"exampleFix":"// before\nTextWriter w = null;\nvar output = new AnsiConsoleOutput(w); // throws\n\n// after\nvar output = new AnsiConsoleOutput(Console.Out);","handlingStrategy":"validation","validationCode":"if (writer is null)\n    writer = Console.Out;\nvar output = new AnsiConsoleOutput(writer);","typeGuard":"static bool IsUsableWriter(TextWriter? w) => w is not null;","tryCatchPattern":"try\n{\n    var output = new AnsiConsoleOutput(writer);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"writer\")\n{\n    writer = Console.Out;\n    var output = new AnsiConsoleOutput(writer);\n}","preventionTips":["Initialize writer variables before constructing AnsiConsoleOutput","Default to Console.Out when the source is absent","Annotate writer sources as non-nullable in your own code"],"tags":["console","io","null-check","argument","ansi-console-output"],"backgroundTag":null,"analyzedSha":"0acc92fada6c42f13984e79c2b5f3d993bdfb099","analyzedAt":"2026-08-13T18:27:21.983Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}