Nullpointer Exception when <Message>.parseFrom(byte [])

712 views
Skip to first unread message

jim horng

unread,
Dec 6, 2010, 5:41:56 AM12/6/10
to Protocol Buffers
Hi all,

It occur to me that when I'm trying to parse any of the binary data
files I listed below,
using <Message>.parseFrom(byte []). it said nullpointer exception.

To my best knowledge, I think when the raw bytes does not contain
required field, protobuf API should return invalidprotobufexception
when using .parseFrom(), or at least it will throw
uninitializeprotobuf exception when .build().
so that client will know what happen and have change to handle them
correctly.
But if it return nullpointerexception, it will be hard to know what
really happen, it can only be roughly assumed message parsing is
failed.
My goal is just to determine if the received binary files is an
expected protobuf message or not.

Please kindly advise or if this is work as design under some reason.
Thanks a lot !



<code snippet>
String filepath = "np1.pb";
byte[] file_byteary = getBytesFromFile(new File(filepath));

xxx.Connection.Builder conn_builder =
xxx.Connection.newBuilder();
xxx.Connection MB_tmp =
xxx.Connection.parseFrom(file_byteary);
conn_builder.mergeFrom(MB_tmp);
System.out.println("is init?" + conn_builder.isInitialized());
assertTrue(conn_builder.build() != null);
</code snippet>

* test data
https://docs.google.com/leaf?id=0B9CkCymjWlZLNWMyMGNiNjEtMDJiZi00NDY0LTgzMWUtMjI1N2I0MWVhNjlh&hl=en

Kenton Varda

unread,
Dec 7, 2010, 10:34:41 PM12/7/10
to jim horng, Protocol Buffers
The file you attached is not useful without source code.

But what would be even better is if you could provide the stack trace for the NullPointerException.  The parser should never throw NPE and I'm not aware of any bugs which cause it to throw NPE.

BTW, it's very easy for bytes which are not actually a protocol buffer to parse correctly as a protocol buffer.  The protobuf encoding is pretty dense.  So I would suggest that you instead use some sort of tagging mechanism to distinguish protobuf data from other kinds of data that you need to process.


--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.


jim horng

unread,
Dec 8, 2010, 10:04:02 AM12/8/10
to Kenton Varda, Protocol Buffers
Hi Kenton,

here are the stacktrace, i found i'm using 2.1.0

java.lang.NullPointerException

        at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:422)

        at com.google.protobuf.GeneratedMessage$Builder.parseUnknownField(GeneratedMessage.java:289)

        at com.trendmicro.mist.proto.MistMessage$MessageBlock$Builder.mergeFrom(MistMessage.java:621)

        at com.trendmicro.mist.proto.MistMessage$MessageBlock$Builder.mergeFrom(MistMessage.java:602)

        at com.trendmicro.mist.proto.MistMessage$MessageBlock$Builder.mergeFrom(MistMessage.java:504)

        at com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:358)

        at com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:351)

        at com.trendmicro.mist.Session.runProducer(Session.java:288)

        at com.trendmicro.mist.Session.run(Session.java:805)

        at java.lang.Thread.run(Thread.java:619)



After I switch to 2.3.0, the exception become much more reasonable:
Exception in thread "main" com.google.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero).
    at com.google.protobuf.InvalidProtocolBufferException.invalidTag(InvalidProtocolBufferException.java:68)
    at com.google.protobuf.CodedInputStream.readTag(CodedInputStream.java:90)
    at test.Test$Person$Builder.mergeFrom(Test.java:279)
    at test.Test$Person$Builder.mergeFrom(Test.java:1)
    at com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:244)
    at com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:183)
    at com.google.protobuf.AbstractMessageLite$Builder.mergeFrom(AbstractMessageLite.java:162)
    at com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:661)
    at com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:183)
    at com.google.protobuf.AbstractMessageLite$Builder.mergeFrom(AbstractMessageLite.java:153)
    at com.google.protobuf.AbstractMessage$Builder.mergeFrom(AbstractMessage.java:654)
    at test.Test$Person.parseFrom(Test.java:119)
    at test.Test_pb_NPE.main(Test_pb_NPE.java:21)

thanks
--
Best Regards,
Jim(洪懷謙)

Kenton Varda

unread,
Dec 14, 2010, 10:48:57 PM12/14/10
to jim horng, Protocol Buffers
Indeed, this was a bug that we fixed.
Reply all
Reply to author
Forward
0 new messages