RicoSuter/NSwag · error · ArgumentNullException
items
Error message
items
What it means
ObservableDictionary.AddRange throws ArgumentNullException when the items dictionary passed in is null. The method requires a non-null IDictionary<TKey,TValue> because it iterates and inspects the items count and keys immediately. This is a fail-fast guard so callers get a clear error at the call site instead of a NullReferenceException deeper in the add logic.
Solutions
- Ensure the source dictionary is initialized before calling AddRange
- Coalesce null to an empty dictionary with ?? new Dictionary<TKey,TValue>()
- Guard with an if (items != null) check before calling AddRange
Example fix
// before
dict.AddRange(externalItems);
// after
if (externalItems != null)
{
dict.AddRange(externalItems);
} Defensive patterns
Strategy: validation
Validate before calling
if (items == null) throw new ArgumentNullException(nameof(items)); // or skip the call dict.AddRange(items);
Type guard
bool canAddRange = items != null;
Try / catch
try { dict.AddRange(items); } catch (ArgumentNullException) { /* initialize items */ } Prevention
- Initialize dictionaries at declaration, never leave them null
- Use ?? new Dictionary<TKey,TValue>() when sourcing from nullable APIs
When it happens
Trigger: Calling AddRange(null) on an ObservableDictionary, e.g. when building a dictionary from a nullable collection or a method that returned null.
Common situations: Populating OpenApiDocument extensions or parameters from a variable that was never initialized, or deserializing optional config that came back null and passing it straight to AddRange.
Related errors
- key
- An item with the same key has already been added.
- document
- globalScopeNames
- This UI does not support multiple documents per UI: Do not…
AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14).
Data as JSON: /api/errors/6840874fdc09db87.
Report an issue: GitHub.
Appendix: source
Thrown at src/NSwag.Core/Collections/ObservableDictionary.cs:76
/// <summary>Initializes a new instance of the <see cref="ObservableDictionary{TKey, TValue}"/> class. </summary>
/// <param name="capacity">The capacity. </param>
/// <param name="comparer">The comparer. </param>
public ObservableDictionary(int capacity, IEqualityComparer<TKey> comparer)
{
_dictionary = new Dictionary<TKey, TValue>(capacity, comparer);
}
/// <summary>Gets the underlying dictonary. </summary>
protected Dictionary<TKey, TValue> Dictionary => _dictionary;
/// <summary>Adds multiple key-value pairs the the dictionary. </summary>
/// <param name="items">The key-value pairs. </param>
public void AddRange(IDictionary<TKey, TValue> items)
{
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
if (items.Count > 0)
{
if (_dictionary.Count > 0)
{
if (items.Keys.Any(k => _dictionary.ContainsKey(k)))
{
throw new ArgumentException("An item with the same key has already been added.");
}
foreach (var pair in items)
{
_dictionary.Add(pair.Key, pair.Value);
}
}
else
{View on GitHub (pinned to 63daf8fcc3)