Search code examples
javaprotocol-buffers

Can Java Protocol Buffers ever return null on the list getter of a repeated field?


Say you have a repeated field.

message Foo {
  optional int32 val = 1;
}

message Bar {
  repeated Foo foo = 1;
}

This will generate the method

List<Foo> getFooList()

Is there any circumstance where getFooList will return null? Or will it always return a List, even if it's empty?


Solution

  • No, there is no case where it returns null. In fact, none of the field accessors on Java protobuf generated classes ever return null; they always return the default value if the field is not present. Similarly, the setters do not allow you to set null.