XINCGer/Unity3DTraining · error · ArgumentException

Sequence contained null element

Error message

Sequence contained null element

What it means

RepeatedField<T>.AddRange validates that the input sequence contains no null elements and throws ArgumentException("Sequence contained null element", "values") otherwise. Null entries are illegal because protobuf repeated fields cannot represent null messages/elements.

Solutions

  1. Filter nulls before adding: values.Where(v => v != null).
  2. Throw or log at the source that produced the null element.
  3. Use a non-nullable element type or provide a default instance for missing entries.
  4. If coming from JSON, pre-process the array to drop null items.

Example fix

// before
message.Items.AddRange(rawItems); // rawItems contains null
// after
message.Items.AddRange(rawItems.Where(i => i != null));
Defensive patterns

Strategy: validation

Validate before calling

if (values == null) throw new ArgumentNullException(nameof(values));
if (values.Any(v => v == null)) throw new ArgumentException("null element in source sequence", nameof(values));
field.AddRange(values);

Type guard

static bool HasNoNulls<T>(IEnumerable<T> values) where T : class => values != null && !values.Any(v => v == null);

Try / catch

try
{
    field.AddRange(values);
}
catch (ArgumentException ex) when (ex.ParamName == "values")
{
    // sanitize and retry
    field.AddRange(values.Where(v => v != null));
}

Prevention

When it happens

Trigger: Calling repeatedField.AddRange(someCollection) where any element of someCollection is null — the pre-scan foreach finds item == null before copying.

Common situations: Building a list with LINQ (e.g. Select returning null for unmatched items) and appending it to a message's repeated field; deserializing from JSON with explicit nulls in an array; legacy code paths that tolerated nulls in List<T>.

Related errors


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

Appendix: source

Thrown at NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Collections/RepeatedField.cs:342

            var collection = values as ICollection;
            if (collection != null)
            {
                var extraCount = collection.Count;
                // For reference types and nullable value types, we need to check that there are no nulls
                // present. (This isn't a thread-safe approach, but we don't advertise this is thread-safe.)
                // We expect the JITter to optimize this test to true/false, so it's effectively conditional
                // specialization.
                if (default(T) == null)
                {
                    // TODO: Measure whether iterating once to check and then letting the collection copy
                    // itself is faster or slower than iterating and adding as we go. For large
                    // collections this will not be great in terms of cache usage... but the optimized
                    // copy may be significantly faster than doing it one at a time.
                    foreach (var item in collection)
                    {
                        if (item == null)
                        {
                            throw new ArgumentException("Sequence contained null element", "values");
                        }
                    }
                }
                EnsureSize(count + extraCount);
                collection.CopyTo(array, count);
                count += extraCount;
                return;
            }

            // We *could* check for ICollection<T> as well, but very very few collections implement
            // ICollection<T> but not ICollection. (HashSet<T> does, for one...)

            // Fall back to a slower path of adding items one at a time.
            foreach (T item in values)
            {
                Add(item);
            }
        }

View on GitHub (pinned to 016f98412e)