Issue 491 in protobuf: Optional field throws NullPointerException when not set values.

1,881 views
Skip to first unread message

prot...@googlecode.com

unread,
Apr 1, 2013, 6:37:53 AM4/1/13
to prot...@googlegroups.com
Status: New
Owner: liu...@google.com
Labels: Type-Defect Priority-Medium

New issue 491 by zhaidang...@gmail.com: Optional field throws
NullPointerException when not set values.
http://code.google.com/p/protobuf/issues/detail?id=491

What steps will reproduce the problem?
1. in proto file i defined a field : optional string test;
2. then generated java classes files.
3. in java file , method setTest() has throw new NullPointerException();

What is the expected output? What do you see instead?
it should not include NullPointerException, because it its optional .
if string field is required , this is right.

What version of the product are you using? On what operating system?
Version 2.4.1 and 2.5.0 exe file on win32 ( windows 7 32bit)

Please provide any additional information below.

file setMethod should not include the code if it is optional

if (value == null) {
throw new NullPointerException();
}

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

prot...@googlecode.com

unread,
Apr 1, 2013, 1:20:35 PM4/1/13
to prot...@googlegroups.com
Updates:
Status: WorkingAsIntended

Comment #1 on issue 491 by liu...@google.com: Optional field throws
NullPointerException when not set values.
http://code.google.com/p/protobuf/issues/detail?id=491

This works as intended. We do not allow setting null to the field. When the
field is absent, it will point to the default instance instead of null. You
need to check if the parameter is null before setting it to the message.

prot...@googlecode.com

unread,
Apr 1, 2013, 10:13:04 PM4/1/13
to prot...@googlegroups.com

Comment #2 on issue 491 by zhaidang...@gmail.com: Optional field throws
NullPointerException when not set values.
http://code.google.com/p/protobuf/issues/detail?id=491

but I did not call setXXX() method, when use the object , it throws
NullPointerException.
If in this case, the required field and optional field is the same ?
I want optional field does not throw NullPointerException when I did not
set values.

prot...@googlecode.com

unread,
Apr 1, 2013, 10:14:34 PM4/1/13
to prot...@googlegroups.com

Comment #3 on issue 491 by zhaidang...@gmail.com: Optional field throws

prot...@googlecode.com

unread,
Apr 2, 2013, 1:51:57 AM4/2/13
to prot...@googlegroups.com

Comment #4 on issue 491 by xiaof...@google.com: Optional field throws
NullPointerException when not set values.
http://code.google.com/p/protobuf/issues/detail?id=491

How did you use the object? Any sample code?

prot...@googlecode.com

unread,
May 29, 2013, 5:07:32 PM5/29/13
to prot...@googlegroups.com

Comment #5 on issue 491 by Danil.Ne...@gmail.com: Optional field throws
NullPointerException when not set values.
http://code.google.com/p/protobuf/issues/detail?id=491

Team, you are checking on null any way in setXXX() methods. Why don't you
set the default instance when null is provided for optional field?

Like the following pseudo code:

if (value == null) {
set default instance.
}

Throwing NPE for optional fields you prevent using chained building of
proto-messages:

Something something = new Something();
something.setAnything(null);

ObjectProto.Object.newBuilder()
.setRequired(new Bar())
.setOptional(new Foo())
.setOptional(something.getAnything())
.build();

prot...@googlecode.com

unread,
May 29, 2013, 5:50:59 PM5/29/13
to prot...@googlegroups.com

Comment #6 on issue 491 by xiaof...@google.com: Optional field throws
NullPointerException when not set values.
http://code.google.com/p/protobuf/issues/detail?id=491

We don't have the intention to change to the current behavior. Passing null
is likely to be a programming error. Allowing it might do more harm than
good.

Andreas V

unread,
Jun 8, 2015, 7:31:40 PM6/8/15
to prot...@googlegroups.com, prot...@googlecode.com, codesite...@google.com
I know its an old thread but i use protobuf3 with Java and it has the same NPE behavior with .set(null-reference). Is their any chance to change this? an option to generating java classes would be nice. It is absolutly annoying to null-check on every .set method. This will lead to more errors in code and expand it inadequate due to chained-setters are not possible. :(

Josh Humphries

unread,
Jun 9, 2015, 1:07:13 PM6/9/15
to Andreas V, prot...@googlegroups.com, prot...@googlecode.com, codesite...@google.com
As a work-around, you can implement a simple protoc plugin that will generate additional null-friendly methods into the classes already created by the Java codegen built into protoc.

That's actually what we've done at Square. As you point out, the null-unfriendliness is particularly burdensome with builders, for cases where you want to use method-chaining but also conditionally set or clear a field.

Our generated messages include getOrNull*() accessor fields (so they return null if unset instead of the field's default value) and the builders have setOrClear*() setter methods (that clear the field when null is passed in).



----
Josh Humphries
Manager, Shared Systems  |  Platform Engineering
Atlanta, GA  |  678-400-4867

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Dean Hiller

unread,
May 1, 2020, 7:57:59 AM5/1/20
to Protocol Buffers
John, that is awesome.  Do you have examples of how you did that? AND can it be done under the covers of doing gRPC which we are looking into?

Also, since protobuf supports json now, I am wondering because of the above issue if it supports "age":null vs "age":0 vs. non existent age in the json?  ie. this post https://groups.google.com/forum/#!topic/protobuf/d51mjUf_w7g

Especially since I just coded up a grpc-json plugin that we are going to start testing out/(and perhaps using) here https://github.com/deanhiller/webpieces/tree/master/webserver-plugins/plugin-grpc-json
We are about to add the binary one as well.

thanks,
Dean


On Tuesday, June 9, 2015 at 11:07:13 AM UTC-6, Josh Humphries wrote:
As a work-around, you can implement a simple protoc plugin that will generate additional null-friendly methods into the classes already created by the Java codegen built into protoc.

That's actually what we've done at Square. As you point out, the null-unfriendliness is particularly burdensome with builders, for cases where you want to use method-chaining but also conditionally set or clear a field.

Our generated messages include getOrNull*() accessor fields (so they return null if unset instead of the field's default value) and the builders have setOrClear*() setter methods (that clear the field when null is passed in).



----
Josh Humphries
Manager, Shared Systems  |  Platform Engineering
Atlanta, GA  |  678-400-4867

On Mon, Jun 8, 2015 at 4:04 AM, Andreas V <andre...@gmail.com> wrote:
I know its an old thread but i use protobuf3 with Java and it has the same NPE behavior with .set(null-reference). Is their any chance to change this? an option to generating java classes would be nice. It is absolutly annoying to null-check on every .set method. This will lead to more errors in code and expand it inadequate due to chained-setters are not possible. :(

Am Mittwoch, 29. Mai 2013 23:50:59 UTC+2 schrieb prot...@googlecode.com:

Comment #6 on issue 491 by xiaof...@google.com: Optional field throws  
NullPointerException when not set values.
http://code.google.com/p/protobuf/issues/detail?id=491

We don't have the intention to change to the current behavior. Passing null  
is likely to be a programming error. Allowing it might do more harm than  
good.

--
You received this message because this project is configured to send all  
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prot...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages