Skip to content

fix(entities): 修复 InterAction.getEnvType 对 chat_type 的空指针 - #1

Open
AnnaofArendelle wants to merge 1 commit into
HuHoBot:masterfrom
AnnaofArendelle:fix/interaction-chat-type-npe
Open

fix(entities): 修复 InterAction.getEnvType 对 chat_type 的空指针#1
AnnaofArendelle wants to merge 1 commit into
HuHoBot:masterfrom
AnnaofArendelle:fix/interaction-chat-type-npe

Conversation

@AnnaofArendelle

Copy link
Copy Markdown

问题

InterAction.chat_type 声明为 Integer,而 getEnvType() 中写的是 chat_type == 0,会触发自动拆箱。QQ 下发的 InterAction 事件并非总是携带该字段,此时抛出 NPE。

崩溃发生在 Events.onEvent日志格式化路径上:

logger.info(String.format("Bot(%s) post(%s) from %s", bot.getInfo().getUsername(), event, event.getClassName()));
//                                                                                  ^^^^^ toString()
java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.chat_type" is null
    at io.github.kloping.qqbot.entities.qqpd.InterAction.getEnvType(InterAction.java:75)
    at io.github.kloping.qqbot.entities.qqpd.InterAction.getCid(InterAction.java:65)
    at io.github.kloping.qqbot.impl.BaseInterActionEvent.toString(BaseInterActionEvent.java:69)
    at java.base/java.util.Formatter$FormatSpecifier.printString(Formatter.java:3292)
    ...
    at io.github.kloping.qqbot.network.Events.onEvent(Events.java:115)
    at io.github.kloping.qqbot.network.Events.lambda$onReceive$0(Events.java:51)

事件本身在这一行之前就已分发完毕,功能不受影响,但每收到一次这类事件就会向 stderr 打印一次完整堆栈。

复现

在真实环境中稳定出现:机器人接入 QQ 群后,群内 @ 机器人或触发交互组件时,日志中周期性出现上述堆栈。本次是在一台 Minecraft 服务端上跑机器人时发现的,一个会话内出现 4 次。

改动

getEnvType() 先判空;chat_type 缺失时回退到 scene 字段 —— 按类上 Javadoc 引用的官方文档,二者承载同一信息(guild / group / c2c)。两者皆缺失时按群聊处理,与 chat_type 非 0 时的既有行为保持一致。

chat_type 非空时行为完全不变,不影响任何现有调用方。

@Override
public EnvType getEnvType() {
    if (chat_type != null) {
        return chat_type == 0 ? EnvType.GUILD : EnvType.GROUP;
    }
    // chat_type 并非每次都会下发;scene 字段承载同一信息(guild / group / c2c),
    // 两者皆缺失时按群聊处理,与 chat_type 非 0 时的既有行为一致。
    return SCENE_GUILD.equals(scene) ? EnvType.GUILD : EnvType.GROUP;
}

验证

已在引入本仓库源码的项目中编译通过(compileJava BUILD SUCCESSFUL)。

备注

刻意保持最小改动,没有一并处理 chat_type == 2(单聊)目前被归入 GROUP 分支、以及 getCid() 在单聊场景返回 group_openid 的问题 —— 那涉及语义判断,留给维护者定夺。

chat_type 声明为 Integer,getEnvType() 中的 `chat_type == 0` 会触发拆箱。
QQ 下发的 InterAction 事件并非总是携带该字段,此时抛出 NullPointerException。

崩溃点在 Events.onEvent 的日志格式化路径:

    logger.info(String.format("Bot(%s) post(%s) from %s", ..., event, ...));
      -> BaseInterActionEvent.toString() -> InterAction.getCid() -> getEnvType()

    java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()"
        because "this.chat_type" is null
        at io.github.kloping.qqbot.entities.qqpd.InterAction.getEnvType(InterAction.java:75)
        at io.github.kloping.qqbot.entities.qqpd.InterAction.getCid(InterAction.java:65)
        at io.github.kloping.qqbot.impl.BaseInterActionEvent.toString(BaseInterActionEvent.java:69)
        at io.github.kloping.qqbot.network.Events.onEvent(Events.java:115)

事件本身已分发完毕,功能不受影响,但每次都会向 stderr 打印一次堆栈。

改为先判空;chat_type 缺失时回退到 scene 字段——按官方文档,二者承载同一信息
(guild / group / c2c)。两者皆缺失时按群聊处理,与 chat_type 非 0 时的既有行为一致。
chat_type 非空时行为完全不变。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant