46

I am using Google Protobuf using java. I wrote a statement like

optional repeated   string  users = 9;

When I tried to compile I am getting an error like

message.proto:39:57: Missing field number.

All I wanted was to create an array of strings.

Can anybody help me to resolve it.

PS: If I avoided optional keyword then it is compiling but in java I am getting a class not found error for com.google.protobuf.ProtocolStringList

Thanks in advance

1
  • 8
    repeated is already optional - it's 0 or more. Sep 3, 2014 at 6:33

2 Answers 2

76

All you need is:

repeated string users = 9;

You don't need the optional modifier, and it looks like it is confusing the parser. A repeated field is inherently optional: you just don't add any values.

As for com.google.protobuf.ProtocolStringList: check that the version of the .proto compiler (protoc) you are using is an exact match for the library version you are using.

7
  • 2
    Thank you for your answer. My compiler version was 2.6 and I was using library 2.5. Now it is working. Thank you very much. Sep 3, 2014 at 6:49
  • @Harikrishnan interesting; did you build the 2.6 compiler yourself? I thought there were only downloads for 2.5? (I'm curious because of keeping an eye on the release of the new oneof feature) Sep 3, 2014 at 6:50
  • @Harikrishnan ah, fair enough; I was only looking at "featured" downloads, but yes, 2.6 is there Sep 3, 2014 at 8:13
  • 4
    So, what if I want to distinguish an empty array (=new type [0]) from no array (=null)? Additional boolean field or is there any better solution?
    – Alex Che
    Mar 31, 2016 at 7:31
  • 1
    @AlexChe they're completely indistinguishable in the wire specification (outside of my control), so : yeah, you'd need to handle that separately. Mar 31, 2016 at 7:34
-3

The generated file contains method for retrieving count. e.g. int getXXXCount(); One issue is that such method wouldn't be available for generated file corresponding to prior versions of the protoc def.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.