spectreconsole/spectre.console · error · ArgumentNullException
Value cannot be null. (Parameter 'obj')
Error message
Value cannot be null. (Parameter 'obj')
What it means
Thrown by the AddNode<T>(this T obj, string markup) extension on IHasTreeNodes when obj is null. The check uses an explicit 'if (obj is null) throw new ArgumentNullException(nameof(obj))' rather than ArgumentNullException.ThrowIfNull. Because it is an extension method, the C# compiler will not surface the null as a normal instance call, so this guard exists to give a clear parameter name.
Source
Thrown at src/Spectre.Console/IHasTreeNodes.cs:31
/// <summary>
/// Contains extension methods for <see cref="IHasTreeNodes"/>.
/// </summary>
public static class HasTreeNodeExtensions
{
/// <summary>
/// Adds a tree node.
/// </summary>
/// <typeparam name="T">An object with tree nodes.</typeparam>
/// <param name="obj">The object to add the tree node to.</param>
/// <param name="markup">The node's markup text.</param>
/// <returns>The added tree node.</returns>
public static TreeNode AddNode<T>(this T obj, string markup)
where T : IHasTreeNodes
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
ArgumentNullException.ThrowIfNull(markup);
return AddNode(obj, new Markup(markup));
}
/// <summary>
/// Adds a tree node.
/// </summary>
/// <typeparam name="T">An object with tree nodes.</typeparam>
/// <param name="obj">The object to add the tree node to.</param>
/// <param name="renderable">The renderable to add.</param>
/// <returns>The added tree node.</returns>
public static TreeNode AddNode<T>(this T obj, IRenderable renderable)
where T : IHasTreeNodes
{
if (obj is null)View on GitHub (pinned to 0acc92fada)
Solutions
- Ensure the Tree/TreeNode you call AddNode on is non-null before the call
- Initialize the variable at declaration (e.g. var tree = new Tree("root"))
- Use a null check or the null-conditional operator with a fallback
Example fix
// before
Tree tree = null;
tree.AddNode("item"); // throws
// after
var tree = new Tree("root");
tree.AddNode("item"); Defensive patterns
Strategy: validation
Validate before calling
if (tree is not null)
tree.AddNode("item"); Type guard
static bool HasTree(T? obj) where T : IHasTreeNodes => obj is not null;
Try / catch
try
{
tree.AddNode("item");
}
catch (ArgumentNullException ex) when (ex.ParamName == "obj")
{
// the receiver was null; initialize a new Tree and retry
} Prevention
- Initialize Tree/TreeNode variables at declaration
- Null-check the receiver before calling extension methods
- Treat a null IHasTreeNodes as a programmer error, not a runtime condition
When it happens
Trigger: Calling .AddNode(markup) on a null IHasTreeNodes reference (e.g. a null Tree or TreeNode variable).
Common situations: A Tree/TreeNode variable that was never assigned; a lookup that returned null; chaining after a method whose result was unexpectedly null.
Related errors
- Value cannot be null. (Parameter 'writer')
- Output writer was null
- Profile enricher of type '{enricher.GetType().FullName}' doe
- Value cannot be null. (Parameter 'value')
- Value cannot be null. (Parameter 'header')
AI-assisted analysis of spectreconsole/spectre.console@0acc92fada (2026-08-13).
Data as JSON: /api/errors/8de3e943af0c687d.
Report an issue: GitHub.