XINCGer/Unity3DTraining · error · ArgumentException

Key is null

Error message

Key is null

What it means

The explicit ICollection<KeyValuePair<TKey,TValue>>.Remove implementation throws ArgumentException when the Key of the pair being removed is null. This is a generic null-input guard: a null key can never match any entry, so the library rejects it upfront instead of performing a lookup.

Solutions

  1. Guard with if (item.Key == null) before calling Remove and skip or log such entries.
  2. Use MapField.Remove(TKey key) or ContainsKey first, ensuring a non-null key.
  3. Ensure pairs are fully constructed rather than default(KeyValuePair<TKey,TValue>) before removal.

Example fix

if (pair.Key != null) { collection.Remove(pair); } // or use mapField.Remove(pair.Key) which handles lookups directly.
Defensive patterns

Strategy: validation

Validate before calling

if (item.Key == null) throw new ArgumentNullException(nameof(item));

Type guard

bool HasKey<TK,TV>(KeyValuePair<TK,TV> p) => p.Key != null;

Try / catch

try { collection.Remove(item); } catch (ArgumentException ex) { /* ex.ParamName == "item" */ }

Prevention

When it happens

Trigger: Calling the ICollection<KeyValuePair<TKey,TValue>>.Remove interface method (e.g. through a MapField typed as that interface) with a KeyValuePair whose Key property is null.

Common situations: Removing entries through an ICollection view of the map where the pair was default-initialized (default(KeyValuePair<TKey,TValue>) has a null key for reference-type keys); iterating data with missing keys and attempting removal.

Related errors


AI-assisted analysis of XINCGer/Unity3DTraining@016f98412e (2026-09-12). Data as JSON: /api/errors/0947ede0a54766fe. Report an issue: GitHub.

Appendix: source

Thrown at NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Collections/MapField.cs:317

        bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
        {
            if (item.Key == null)
            {
                throw new ArgumentException("Key is null", "item");
            }
            LinkedListNode<KeyValuePair<TKey, TValue>> node;
            if (map.TryGetValue(item.Key, out node) &&
                EqualityComparer<TValue>.Default.Equals(item.Value, node.Value.Value))
            {
                map.Remove(item.Key);
                node.List.Remove(node);
                return true;
            }

View on GitHub (pinned to 016f98412e)