【问题标题】:Protobuf / Protocol Buffers - IndexOutOfBoundsExceptionProtobuf / 协议缓冲区 - IndexOutOfBoundsException
【发布时间】:2012-10-18 12:50:42
【问题描述】:

我正在开发一个使用 Google 协议缓冲区的客户端-服务器应用程序。 不幸的是,当我使用构建器模式在服务器端构建协议缓冲区响应时,我得到一个 IndexOutOfBoundsException:

这是我构建 protobuf 文件的行:

Builder getVGResonseBuilder = App_getVGResponse.GetVGResponse.newBuilder().getVGBuilder(0);
[some more code that uses the builder patterns]
getVGResponseBuilder.set...
getVGResponseBuilder.set...

错误发生在第一行代码。

这里是protobuf的定义(ofc我已经编译好了!编译的cals是App_getVGResponse):

message GetVGResponse {

    message VG {
        optional string id = 1;
        optional string g_id = 2;
        optional int64 f_id = 3;
        optional string g_name = 4;
    }

    repeated VG v_gp = 1;
}

这是我的堆栈跟踪的摘录

Exception in thread "main" com.google.protobuf.InvalidProtocolBufferException: **Protocol message tag had invalid wire type.**
    at com.google.protobuf.InvalidProtocolBufferException.invalidWireType(InvalidProtocolBufferException.java:78)
    at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:498)
    at com.google.protobuf.GeneratedMessage$Builder.parseUnknownField(GeneratedMessage.java:439)

并且在运行时调试器会显示 ma 变量:

e
-> cause: IndexOutOfBoundsException (id=12291)
-> detaiMessage: Index: 0, Size: 0 (id=12324)
-> stackTrace null

【问题讨论】:

  • 这是哪个实现?
  • 我不完全了解您需要哪些信息。服务器和客户端都运行 java 代码。我使用 protobuf 2.4.1 库。
  • 这就是我要问的问题;有很多实现(一些来自 Google,一些来自社区),并且许多实现将具有非常相似的使用语义。知道您使用的是 java 版本可以大大缩小范围!
  • 将堆栈跟踪和运行时变量添加到初始问题

标签: java buffer protocols protocol-buffers indexoutofboundsexception


【解决方案1】:

我个人创建了“子构建器”,然后将其添加到父构建器。即

    App_GetVGResponse.GetVGResponse.Builder bldr = App_GetVGResponse.GetVGResponse.newBuilder();
    App_GetVGResponse.GetVGResponse.VG.Builder childBldr = App_GetVGResponse.GetVGResponse.VG.newBuilder();

    childBldr.setId(value);
            ...........

    bldr.addVGp(childBldr);

我认为错误是因为您在添加之前获得了“子”构建器

【讨论】:

    猜你喜欢
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 2021-12-19
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多