Nullpointer Exception when printing/accessing Protocol Buffer
I get the following NullPointerException when trying to print out a protocol buffer:
Exception in thread "main" java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.protobuf.GeneratedMessage.invokeOrDie(GeneratedMessage.java:895)
at com.google.protobuf.GeneratedMessage.access$1400(GeneratedMessage.java:55)
at com.google.protobuf.GeneratedMessage$FieldAccessorTable$SingularEnumFieldAccessor.get(GeneratedMessage.java:1145)
at com.google.protobuf.GeneratedMessage.getField(GeneratedMessage.java:127)
at com.google.protobuf.GeneratedMessage.getAllFieldsMutable(GeneratedMessage.java:84)
at com.google.protobuf.GeneratedMessage.getAllFields(GeneratedMessage.java:119)
at com.google.protobuf.TextFormat.print(TextFormat.java:109)
at com.google.protobuf.TextFormat.print(TextFormat.java:64)
at com.google.protobuf.TextFormat.printToString(TextFormat.java:81)
at com.google.protobuf.AbstractMessage.toString(AbstractMessage.java:82)
at java.lang.String.valueOf(String.java:2826)
at java.io.PrintStream.println(PrintStream.java:771)
All I'm doing is printing out the protocol buffer, it's literally:
NameOfProtocolBuff开发者_如何学编程er.Builder a = NameOfProtocolBuffer.newBuilder();
// Set some fields....
NameOfProtocolBuffer b = a.build();
System.out.println(b); // etc.
All the fields of the protocol buffer either optional or repeated.
Thanks for any help!
It looks like the exception is thrown from the toString() method of your 'NameOfProtocolBuffer' instance b. Try putting a breakpoint at the start of that method. Also check for null on the return value of NameOfProtocolBuffer.newBuilder() and a.build()
精彩评论