AvaloniaUI/Avalonia · error · ArgumentNullException
Value cannot be null. (Parameter 'item')
Error message
Value cannot be null. (Parameter 'item')
What it means
CompositeDisposable.Add(item) appends a disposable to the composite (or disposes it immediately if the composite is already disposed). It throws ArgumentNullException(nameof(item)) when item is null, preserving the no-null-disposables invariant at the mutation boundary.
Source
Thrown at src/Avalonia.Base/Reactive/CompositeDisposable.cs:111
return list;
}
/// <summary>
/// Gets the number of disposables contained in the <see cref="CompositeDisposable"/>.
/// </summary>
public int Count => Volatile.Read(ref _count);
/// <summary>
/// Adds a disposable to the <see cref="CompositeDisposable"/> or disposes the disposable if the <see cref="CompositeDisposable"/> is disposed.
/// </summary>
/// <param name="item">Disposable to add.</param>
/// <exception cref="ArgumentNullException"><paramref name="item"/> is <c>null</c>.</exception>
public void Add(IDisposable item)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
lock (_gate)
{
if (!_disposed)
{
_disposables.Add(item);
// If read atomically outside the lock, it should be written atomically inside
// the plain read on _count is fine here because manipulation always happens
// from inside a lock.
Volatile.Write(ref _count, _count + 1);
return;
}
}
item.Dispose();
}View on GitHub (pinned to 11c5427268)
Solutions
- Null-check before Add: if (d is not null) composite.Add(d);.
- Ensure subscription helpers always return a non-null IDisposable (use Disposable.Empty for no-ops).
- Prefer the params/IList constructor for bulk adds so nulls surface at one validated point.
Example fix
// before composite.Add(maybeNullDisposable); // after if (maybeNullDisposable is not null) composite.Add(maybeNullDisposable);
Defensive patterns
Strategy: validation
Validate before calling
if (item is not null) composite.Add(item);
Type guard
bool IsDisposable(IDisposable? d) => d is not null;
Prevention
- Guard Add with a null check.
- Ensure subscription helpers return non-null disposables.
- Use the validated bulk constructors for groups.
When it happens
Trigger: Calling composite.Add(null), or Add-ing a disposable obtained from a subscription/factory that returned null.
Common situations: Adding a subscription token whose creation method returned null, or a conditional add where the disposable was not created.
Related errors
- Value cannot be null. (Parameter 'disposables')
- Value cannot be null. (Parameter 'array')
- Value cannot be null. (Parameter 'tcs')
- Value cannot be null. (Parameter 'onNext')
- Value cannot be null. (Parameter 'onError')
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/8b994fe5289b82b4.
Report an issue: GitHub.