somebody explain me how can handle null values in google protobuf .
i have structure like this :
syntax = "proto3";
import "google/protobuf/wrappers.proto";
message Person {
google.protobuf.DoubleValue age = 4;
}
and java code :
DoubleValue myAge = null;
Person build = Person.newBuilder()
.setAge(myAge) // <- NPE
.build();
this code result null pointer exception on .setAge(myAge)
line.
so what is use case of protobuf DoubleValue ? I thought it was used to manage null values . but still receive NullPointerException .
Do you use code like this for handle this problem ?
DoubleValue a = null;
Person.Builder builder = Person.newBuilder();
if (a != null) {
builder.setAge(a);
}
Person build = builder.build();
UPDATE:
this is part of code generated by protoc command :
/**
* <code>.google.protobuf.DoubleValue age = 9;</code>
*/
public Builder setAge(com.google.protobuf.DoubleValue value) {
if (ageBuilder_ == null) {
if (value == null) { //<-- Additional statement
throw new NullPointerException();
}
age_ = value;
onChanged();
} else {
ageBuilder_.setMessage(value);
}
Why set additional if
statement in generated code ?
I think this is a bug for google protocol buffer.
finally i wrote this code:
DoubleValue.Builder builder = DoubleValue.newBuilder();
DoubleValue myAge = null;
Person build = Person.newBuilder()
.setAge(myAge != null ? myAge : builder )
.build();