spectreconsole/spectre.console · error · ArgumentNullException
Value cannot be null. (Parameter 'writer')
Error message
Value cannot be null. (Parameter 'writer')
What it means
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.
Source
Thrown at src/Spectre.Console/AnsiConsoleOutput.cs:42
}
return false;
}
}
/// <inheritdoc/>
public int Width => ConsoleHelper.GetSafeWidth();
/// <inheritdoc/>
public int Height => ConsoleHelper.GetSafeHeight();
/// <summary>
/// Initializes a new instance of the <see cref="AnsiConsoleOutput"/> class.
/// </summary>
/// <param name="writer">The output writer.</param>
public AnsiConsoleOutput(TextWriter writer)
{
Writer = writer ?? throw new ArgumentNullException(nameof(writer));
}
/// <inheritdoc/>
public void SetEncoding(Encoding encoding)
{
if (Writer.IsStandardOut() || Writer.IsStandardError())
{
System.Console.OutputEncoding = encoding;
}
}
}View on GitHub (pinned to 0acc92fada)
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
Example fix
// before TextWriter w = null; var output = new AnsiConsoleOutput(w); // throws // after var output = new AnsiConsoleOutput(Console.Out);
Defensive patterns
Strategy: validation
Validate before calling
if (writer is null)
writer = Console.Out;
var output = new AnsiConsoleOutput(writer); Type guard
static bool IsUsableWriter(TextWriter? w) => w is not null;
Try / catch
try
{
var output = new AnsiConsoleOutput(writer);
}
catch (ArgumentNullException ex) when (ex.ParamName == "writer")
{
writer = Console.Out;
var output = new AnsiConsoleOutput(writer);
} Prevention
- 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
When it happens
Trigger: Calling new AnsiConsoleOutput(null) or otherwise constructing it with a null TextWriter reference.
Common situations: 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.
Related errors
- Output writer was null
- Value cannot be null. (Parameter 'obj')
- Cannot convert color to console color.
- Profile enricher of type '{enricher.GetType().FullName}' doe
- Value cannot be null. (Parameter 'value')
AI-assisted analysis of spectreconsole/spectre.console@0acc92fada (2026-08-13).
Data as JSON: /api/errors/d22aed76f774a3f8.
Report an issue: GitHub.