PrismLibrary/Prism · error · ArgumentNullException

key

Error message

key

What it means

A generic validation guard in ListDictionary<TKey,TValue>.Add(TKey): the dictionary cannot create or look up a list without a key, so a null key argument throws ArgumentNullException naming 'key'. The input at fault is the null key passed to Add.

Solutions

  1. Ensure the key variable is initialized before calling Add
  2. Add a null check/guard at the call site

Example fix

// before
listDictionary.Add(topic);
// after
if (topic == null) throw new InvalidOperationException("Topic must be set");
listDictionary.Add(topic);
Defensive patterns

Strategy: validation

Validate before calling

if (key == null) throw new InvalidOperationException("key must be non-null before ListDictionary.Add");

Type guard

static bool ValidKey<TKey>(TKey key) => key != null;

Try / catch

try { dict.Add(key); }
catch (ArgumentNullException) { /* log and skip null key */ }

Prevention

When it happens

Trigger: Calling ListDictionary.Add(null) directly, or indirectly when an upstream producer inserts events/handlers keyed by a null key.

Common situations: Event aggregation registration code where a topic/type key variable was never initialized.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/2415023d32d59d1b. Report an issue: GitHub.

Appendix: source

Thrown at src/Prism.Core/Common/ListDictionary.cs:21

    /// <summary>
    /// A dictionary of lists.
    /// </summary>
    /// <typeparam name="TKey">The key to use for lists.</typeparam>
    /// <typeparam name="TValue">The type of the value held by lists.</typeparam>
    public sealed class ListDictionary<TKey, TValue> : IDictionary<TKey, IList<TValue>>
    {
        readonly Dictionary<TKey, IList<TValue>> innerValues = [];

        #region Public Methods

        /// <summary>
        /// If a list does not already exist, it will be created automatically.
        /// </summary>
        /// <param name="key">The key of the list that will hold the value.</param>
        public void Add(TKey key)
        {
            if (key == null)
                throw new ArgumentNullException(nameof(key));

            CreateNewList(key);
        }

        /// <summary>
        /// Adds a value to a list with the given key. If a list does not already exist,
        /// it will be created automatically.
        /// </summary>
        /// <param name="key">The key of the list that will hold the value.</param>
        /// <param name="value">The value to add to the list under the given key.</param>
        public void Add(TKey key, TValue value)
        {
            if (key == null)
                throw new ArgumentNullException(nameof(key));

            if (value == null)
                throw new ArgumentNullException(nameof(value));

View on GitHub (pinned to 358118cd64)