AvaloniaUI/Avalonia · error · ArgumentException
Disposables can't contain null
Error message
Disposables can't contain null
What it means
CompositeDisposable.ToList helper copies the source disposables and, per the doc contract, throws ArgumentException('Disposables can't contain null', nameof(disposables)) if any element is null. The copy and null-check are fused into one loop for efficiency. This enforces the invariant that the composite never stores null disposables.
Source
Thrown at src/Avalonia.Base/Reactive/CompositeDisposable.cs:88
private static List<IDisposable?> ToList(IEnumerable<IDisposable> disposables)
{
var capacity = disposables switch
{
IDisposable[] a => a.Length,
ICollection<IDisposable> c => c.Count,
_ => 12
};
var list = new List<IDisposable?>(capacity);
// do the copy and null-check in one step to avoid a
// second loop for just checking for null items
foreach (var d in disposables)
{
if (d == null)
{
throw new ArgumentException("Disposables can't contain null", nameof(disposables));
}
list.Add(d);
}
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>View on GitHub (pinned to 11c5427268)
Solutions
- Filter nulls before construction: disposables.Where(d => d is not null).
- Fix the producing code so it never returns null disposables.
- If an absent disposable is legitimate, substitute Disposable.Empty rather than null.
Example fix
// before var cd = new CompositeDisposable(maybeWithNulls); // after var cd = new CompositeDisposable(maybeWithNulls.Where(d => d is not null).ToArray());
Defensive patterns
Strategy: validation
Validate before calling
var clean = disposables.Where(d => d is not null).ToArray(); var cd = new CompositeDisposable(clean);
Type guard
bool NoNulls(IEnumerable<IDisposable?> ds) => ds.All(d => d is not null);
Prevention
- Filter nulls out of disposable collections before construction.
- Fix factories that return null disposables; substitute Disposable.Empty.
- Add an Assert.All(items, i => Assert.NotNull(i)) in tests.
When it happens
Trigger: Constructing CompositeDisposable with any collection (params array or IList) whose enumeration contains at least one null element, e.g. new CompositeDisposable(d1, null, d3).
Common situations: A disposable returned null from a factory (e.g. a subscribe that returned null), a LINQ Select producing nulls, or an array initializer with an accidental null slot.
Related errors
- Specified argument was out of the range of valid values. (Pa
- Value cannot be null. (Parameter 'disposables')
- Value cannot be null. (Parameter 'item')
- Value cannot be null. (Parameter 'array')
- Specified argument was out of the range of valid values. (Pa
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/f7b3d46e5ac2c6bb.
Report an issue: GitHub.