打印/访问协议缓冲区时出现Nullpointer异常

时间:2011-08-26 23:24:32

标签: java nullpointerexception protocol-buffers

尝试打印协议缓冲区时出现以下NullPointerException:

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)

我正在做的就是打印出协议缓冲区,它的字面意思是:

NameOfProtocolBuffer.Builder a = NameOfProtocolBuffer.newBuilder();
// Set some fields....
NameOfProtocolBuffer b = a.build();
System.out.println(b); // etc.

协议缓冲区的所有字段都是可选的或重复的。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

看起来像'NameOfProtocolBuffer'实例b的toString()方法抛出异常。尝试在该方法的开头添加断点。还要检查NameOfProtocolBuffer.newBuilder()和a.build()的返回值是否为null