XINCGer/Unity3DTraining · error · InvalidProtocolBufferException

Repeated field elements cannot be null

Error message

Repeated field elements cannot be null

What it means

Canonical proto3 JSON forbids null inside repeated fields (null is only allowed for null-able value fields like google.protobuf.Value or wrappers at the element position of singular fields... but repeated elements must be concrete values). JsonParser stops array parsing on an EndArray token; if the next token is Null, it throws InvalidProtocolBufferException.

Solutions

  1. Remove null entries from the array before parsing — omit them or use a wrapper type (google.protobuf.Int32Value etc.) if null must be representable
  2. Fix the producer to skip null/undefined elements when building the JSON array
  3. Pre-filter nulls in a normalization step before calling JsonParser for untrusted input
  4. If nullable elements are genuinely required, change the field to repeated wrapper/Value type

Example fix

// before
{ "ids": [1, null, 3] }
// after
{ "ids": [1, 3] }  // or repeated google.protobuf.Int32Value to allow null
Defensive patterns

Strategy: validation

Validate before calling

// Strip null entries from arrays before parsing
static System.Text.Json.JsonElement DropNullArrayElements(System.Text.Json.JsonElement arr) => arr; // filter with: arr.EnumerateArray().Where(e => e.ValueKind != JsonValueKind.Null)

Try / catch

try { return parser.Parse<T>(json); } catch (InvalidProtocolBufferException ex) when (ex.Message == "Repeated field elements cannot be null") { log.Error("null element inside a repeated field"); throw new BadRequestException(ex.Message, ex); }

Prevention

When it happens

Trigger: Parsing JSON like {"items": [1, null, 3]} for a repeated int32/string/message field; clients serializing empty/absent values as null inside arrays; generic serializers mapping undefined list entries to null.

Common situations: JavaScript/Python code building arrays with undefined/None entries; JSON produced from sparse data structures; missing-field semantics collapsed into null by an intermediate layer.

Understand the failure class

Background: "cannot parse invalid wire-format data", "cannot unmarshal", "failed unmarshalling": protobuf unmarshal errors explained — this error's family across 10 libraries.

Related errors


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

Appendix: source

Thrown at NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs:265

        {
            var token = tokenizer.Next();
            if (token.Type != JsonToken.TokenType.StartArray)
            {
                throw new InvalidProtocolBufferException("Repeated field value was not an array. Token type: " + token.Type);
            }

            IList list = (IList)field.Accessor.GetValue(message);
            while (true)
            {
                token = tokenizer.Next();
                if (token.Type == JsonToken.TokenType.EndArray)
                {
                    return;
                }
                tokenizer.PushBack(token);
                if (token.Type == JsonToken.TokenType.Null)
                {
                    throw new InvalidProtocolBufferException("Repeated field elements cannot be null");
                }
                list.Add(ParseSingleValue(field, tokenizer));
            }
        }

        private void MergeMapField(IMessage message, FieldDescriptor field, JsonTokenizer tokenizer)
        {
            // Map fields are always objects, even if the values are well-known types: ParseSingleValue handles those.
            var token = tokenizer.Next();
            if (token.Type != JsonToken.TokenType.StartObject)
            {
                throw new InvalidProtocolBufferException("Expected an object to populate a map");
            }

            var type = field.MessageType;
            var keyField = type.FindFieldByNumber(1);
            var valueField = type.FindFieldByNumber(2);
            if (keyField == null || valueField == null)

View on GitHub (pinned to 016f98412e)