protobuf NPE

What language does this apply to?
Java

If it’s a proto syntax change, is it for proto2 or proto3?
proto3

If it’s about generated code change, what programming language?
Java

Describe the problem you are trying to solve.
For the message below,

message Position {
    string portfolio =1;
}

The generated setter would be something like this

      public Builder setPortfolio(
          java.lang.String value) {
        if (value == null) {
         throw new NullPointerException();
        }
  
        portfolio_ = value;
        onChanged();
        return this;
      }

There is a throw NPE within the method.

I think this is really an opinioned approach, which instead should leave to developers to decide whether to handle it or throw NPE.
There could be a position message, for example, with many known optional field which could be null. Developers should be in a better position on how those fields should be set.

Describe the solution you’d like

The generated class should take the value to be set as it is. Something like

      public Builder setPortfolio(
          java.lang.String value) {
//        if (value == null) {
//         throw new NullPointerException();
//        }
  
        portfolio_ = value;
        onChanged();
        return this;
      }

Describe alternatives you’ve considered

Additional context
Add any other context or screenshots about the feature request here.

I guess the current “opinioned” approach probably could be due to a constraint from the protobuf format, where an int was used to determine the length-delimited value’s length. If no, I think by introducing a negative int (-1) could tell whether the following value is really empty (0) or null (-1).

https://github.com/protocolbuffers/protobuf/issues/9207

Leave a comment