stride3d/stride · error · ArgumentNullException

ArgumentNullException

Error message

ArgumentNullException

What it means

The SortedDictionary.KeyCollection constructor throws ArgumentNullException (without a parameter name) when the dictionary argument is null. A key collection is a live view over a dictionary and cannot exist without one.

Solutions

  1. Initialize the SortedDictionary before accessing .Keys
  2. Null-check the dictionary before using its KeyCollection
  3. Wrap dictionary creation in the type's initialization logic

Example fix

// before
SortedDictionary<string,int> dict;
var keys = new SortedDictionary<string,int>.KeyCollection(dict); // null
// after
var dict = new SortedDictionary<string,int>();
var keys = dict.Keys;
Defensive patterns

Strategy: validation

Validate before calling

if (dict == null) throw new InvalidOperationException("Dictionary must be initialized before accessing Keys");

Type guard

TKey[] SafeKeys<TKey,TValue>(SortedDictionary<TKey,TValue> d) => d?.Keys?.ToArray();

Try / catch

try { var keys = new SortedDictionary<TKey,TValue>.KeyCollection(dict); } catch (ArgumentNullException) { dict = new SortedDictionary<TKey,TValue>(); }

Prevention

When it happens

Trigger: Calling new dictionary.Keys-style construction is not user-facing, but any code path that builds a KeyCollection from a null SortedDictionary reference — usually a dictionary variable that was never initialized.

Common situations: A SortedDictionary field defaulting to null, then passing it into APIs that construct key collections.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/afbb836aec88e78f. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/SortedDictionary.cs:510

                    if (NotStartedOrEnded)
                    {
                        throw new InvalidOperationException();
                    }

                    return new DictionaryEntry(Current.Key, Current.Value);
                }
            }
        }

        public sealed class KeyCollection : ICollection<TKey>, ICollection
        {
            private readonly SortedDictionary<TKey, TValue> dictionary;

            public KeyCollection(SortedDictionary<TKey, TValue> dictionary)
            {
                if (dictionary == null)
                {
                    throw new ArgumentNullException();
                }
                this.dictionary = dictionary;
            }

            public Enumerator GetEnumerator()
            {
                return new Enumerator(dictionary);
            }

            IEnumerator<TKey> IEnumerable<TKey>.GetEnumerator()
            {
                return new Enumerator(dictionary);
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return new Enumerator(dictionary);
            }

View on GitHub (pinned to 96fad776d2)