{"record":{"id":"81d2f8dfc885ff69","repo":"XINCGer/Unity3DTraining","slug":"sequence-contained-null-element","errorCode":null,"errorMessage":"Sequence contained null element","messagePattern":"Sequence contained null element","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Collections/RepeatedField.cs","lineNumber":342,"sourceCode":"            var collection = values as ICollection;\n            if (collection != null)\n            {\n                var extraCount = collection.Count;\n                // For reference types and nullable value types, we need to check that there are no nulls\n                // present. (This isn't a thread-safe approach, but we don't advertise this is thread-safe.)\n                // We expect the JITter to optimize this test to true/false, so it's effectively conditional\n                // specialization.\n                if (default(T) == null)\n                {\n                    // TODO: Measure whether iterating once to check and then letting the collection copy\n                    // itself is faster or slower than iterating and adding as we go. For large\n                    // collections this will not be great in terms of cache usage... but the optimized\n                    // copy may be significantly faster than doing it one at a time.\n                    foreach (var item in collection)\n                    {\n                        if (item == null)\n                        {\n                            throw new ArgumentException(\"Sequence contained null element\", \"values\");\n                        }\n                    }\n                }\n                EnsureSize(count + extraCount);\n                collection.CopyTo(array, count);\n                count += extraCount;\n                return;\n            }\n\n            // We *could* check for ICollection<T> as well, but very very few collections implement\n            // ICollection<T> but not ICollection. (HashSet<T> does, for one...)\n\n            // Fall back to a slower path of adding items one at a time.\n            foreach (T item in values)\n            {\n                Add(item);\n            }\n        }","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/Collections/RepeatedField.cs#L324-L360","documentation":"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.","triggerScenarios":"Calling repeatedField.AddRange(someCollection) where any element of someCollection is null — the pre-scan foreach finds item == null before copying.","commonSituations":"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>.","solutions":["Filter nulls before adding: values.Where(v => v != null).","Throw or log at the source that produced the null element.","Use a non-nullable element type or provide a default instance for missing entries.","If coming from JSON, pre-process the array to drop null items."],"exampleFix":"// before\nmessage.Items.AddRange(rawItems); // rawItems contains null\n// after\nmessage.Items.AddRange(rawItems.Where(i => i != null));","handlingStrategy":"validation","validationCode":"if (values == null) throw new ArgumentNullException(nameof(values));\nif (values.Any(v => v == null)) throw new ArgumentException(\"null element in source sequence\", nameof(values));\nfield.AddRange(values);","typeGuard":"static bool HasNoNulls<T>(IEnumerable<T> values) where T : class => values != null && !values.Any(v => v == null);","tryCatchPattern":"try\n{\n    field.AddRange(values);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"values\")\n{\n    // sanitize and retry\n    field.AddRange(values.Where(v => v != null));\n}","preventionTips":["Sanitize sequences with Where(v => v != null) before AddRange.","Avoid LINQ projections that can yield null into repeated fields.","Strip explicit nulls from arrays before JSON parsing into messages.","Enable nullable reference types to catch nulls at compile time."],"tags":["protobuf","repeatedfield","null-element"],"backgroundTag":"null-argument","analyzedSha":"016f98412ea5e11756b286736f479b66ab6216d5","analyzedAt":"2026-09-12T01:08:58.711Z","contentChangedAt":"2026-09-12T01:08:58.711Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}