Skip to content

Conversation

@mifengjun
Copy link

fix: When chat event type is conversation.chat.completed, a NPE will be thrown BUG;

Error occurred: Cannot invoke "com.coze.openapi.client.connversations.message.model.Message.getType()" because the return value of "com.coze.openapi.client.chat.model.ChatEvent.getMessage()" is null

…be thrown;

Error occurred: Cannot invoke "com.coze.openapi.client.connversations.message.model.Message.getType()" because the return value of "com.coze.openapi.client.chat.model.ChatEvent.getMessage()" is null
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai
Copy link

coderabbitai bot commented Oct 29, 2025

Walkthrough

Adds null-safety check for event.getMessage() in the CONVERSATION_CHAT_COMPLETED handler of StreamChatExample. The code now verifies both that the message is non-null and its type is FOLLOW_UP before accessing content; otherwise it prints token usage.

Changes

Cohort / File(s) Summary
Null-safety enhancement in streaming chat handler
example/src/main/java/example/chat/StreamChatExample.java
Adds defensive null-check for event.getMessage() before accessing type and content fields; implements fallback to token usage printing when message is null or type is not FOLLOW_UP

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5–10 minutes

  • Single file with localized change in event handler logic
  • Straightforward null-safety check addition with no structural complexity

Poem

🐰 A careful hop through null-checks we go,
No crashes here from what we don't know,
The message checked before we peek inside,
Defensive code keeps bugs away to hide!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The pull request title "fix: When chat event type is conversation.chat.completed, a NPE will …" directly corresponds to the main change in the changeset, which adds a null-safety check to prevent a NullPointerException when event.getMessage() is called in the CONVERSATION_CHAT_COMPLETED event handler. The title is specific, clear, and concise, effectively communicating both the problem (NPE) and the context (conversation.chat.completed event type) in a single sentence that a teammate scanning history would immediately understand.
Description Check ✅ Passed The pull request description is directly related to the changeset, describing the specific bug being fixed: a NullPointerException that occurs when event.getMessage() returns null during conversation.chat.completed event processing. The description includes the actual error message encountered, providing clear context about why the null-safety check was needed. This meets the lenient pass criteria for the description check as it meaningfully relates to the changes being made.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
example/src/main/java/example/chat/StreamChatExample.java (2)

57-59: Add null-safety check for event.getMessage().

Similar to the fix applied for CONVERSATION_CHAT_COMPLETED, this handler accesses event.getMessage().getContent() without verifying that getMessage() is non-null. This could throw a NPE if the API returns a CONVERSATION_MESSAGE_DELTA event with a null message.

Apply this diff to add the null check:

 if (ChatEventType.CONVERSATION_MESSAGE_DELTA.equals(event.getEvent())) {
-  System.out.print(event.getMessage().getContent());
+  if (event.getMessage() != null) {
+    System.out.print(event.getMessage().getContent());
+  }
 }

64-66: Add defensive null checks for nested method calls.

The code accesses event.getChat().getUsage().getTokenCount() without verifying that either getChat() or getUsage() returns a non-null value. This could throw a NPE if the API returns null for either of these methods.

Apply this diff to add defensive checks:

 } else {
-  System.out.println("Token usage:" + event.getChat().getUsage().getTokenCount());
+  if (event.getChat() != null && event.getChat().getUsage() != null) {
+    System.out.println("Token usage:" + event.getChat().getUsage().getTokenCount());
+  }
 }
🧹 Nitpick comments (1)
example/src/main/java/example/chat/StreamChatExample.java (1)

79-81: Consider translating comments to English for consistency.

The comments on lines 79 and 81 are in Chinese, while the rest of the codebase uses English. For better maintainability and consistency, consider translating them.

Apply this diff:

-    // 为了防止程序立即退出,添加一个简单的等待
+    // Add a simple wait to prevent the program from exiting immediately
     try {
-      Thread.sleep(5000); // 等待5秒钟
+      Thread.sleep(5000); // Wait for 5 seconds
     } catch (InterruptedException e) {
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a1dad15 and 8d985cb.

📒 Files selected for processing (1)
  • example/src/main/java/example/chat/StreamChatExample.java (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: test (Java 17 on macOS)
  • GitHub Check: test (Java 17 on Windows)
  • GitHub Check: test (Java 11 on Windows)
  • GitHub Check: test (Java 11 on macOS)
🔇 Additional comments (2)
example/src/main/java/example/chat/StreamChatExample.java (2)

61-62: LGTM!

The null-safety check correctly addresses the NPE described in the PR. The code now safely verifies that getMessage() is non-null before accessing its methods.


68-70: LGTM!

The DONE event handler correctly shuts down the executor with no null-safety concerns.

@mifengjun
Copy link
Author

不知道要怎么做才可以通过 ci

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.

2 participants