JamesNK/Newtonsoft.Json · error · ArgumentNullException

array

Error message

array

What it means

ArgumentNullException thrown by JObject's ICollection<KeyValuePair<string,JToken?>>.CopyTo when the destination array is null. The check guards the destination array parameter before any copy occurs.

Source

Thrown at Src/Newtonsoft.Json/Linq/JObject.cs:671

            RemoveAll();
        }

        bool ICollection<KeyValuePair<string, JToken?>>.Contains(KeyValuePair<string, JToken?> item)
        {
            JProperty? property = Property(item.Key, StringComparison.Ordinal);
            if (property == null)
            {
                return false;
            }

            return (property.Value == item.Value);
        }

        void ICollection<KeyValuePair<string, JToken?>>.CopyTo(KeyValuePair<string, JToken?>[] array, int arrayIndex)
        {
            if (array == null)
            {
                throw new ArgumentNullException(nameof(array));
            }
            if (arrayIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(arrayIndex), "arrayIndex is less than 0.");
            }
            if (arrayIndex >= array.Length && arrayIndex != 0)
            {
                throw new ArgumentException("arrayIndex is equal to or greater than the length of array.");
            }
            if (Count > array.Length - arrayIndex)
            {
                throw new ArgumentException("The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.");
            }

            int index = 0;
            foreach (JProperty property in _properties)
            {
                array[arrayIndex + index] = new KeyValuePair<string, JToken?>(property.Name, property.Value);

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Allocate the destination array before calling CopyTo: new KeyValuePair<string,JToken?>[jObject.Count].
  2. Avoid CopyTo entirely and materialize via LINQ: jObject.Properties().Select(p => new KeyValuePair<string,JToken?>(p.Name, p.Value)).ToArray().
  3. Null-check the array before dispatching in shared helpers.

Example fix

// before
var arr = (KeyValuePair<string, JToken?>[])null;
((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(arr, 0);

// after
var arr = new KeyValuePair<string, JToken?>[jObject.Count];
((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(arr, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null) throw new ArgumentNullException(nameof(array));
((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(array, arrayIndex);

Try / catch

try { ((ICollection<KeyValuePair<string,JToken?>>)jObject).CopyTo(arr, idx); }
catch (ArgumentNullException) { arr = new KeyValuePair<string, JToken?>[jObject.Count]; }

Prevention

When it happens

Trigger: Calling ((ICollection<KeyValuePair<string,JToken>>)jObject).CopyTo(null, index), or passing a null array through a generic collection-copy routine, a CollectionEditor, or LINQ-to-ICollection helpers.

Common situations: Generic helpers that copy ICollection contents to a caller-supplied buffer where the buffer was not allocated; binding/designer code that passes null expecting lazy allocation.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/5ed97c03c1e712a4. Report an issue: GitHub.