Skip to content

Conversation

@mifengjun
Copy link

fix: When chat event type is conversation.chat.completed, a NPE will 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

…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 a null check for event.getMessage() before accessing its type in the CONVERSATION_CHAT_COMPLETED branch to prevent potential NullPointerExceptions when a message is absent.

Changes

Cohort / File(s) Summary
Null Safety Enhancement
example/src/main/java/example/chat/StreamChatExample.java
Added null check for event.getMessage() before type access in CONVERSATION_CHAT_COMPLETED handler to prevent NullPointerException

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

  • Minimal scope: single file with localized defensive check
  • Straightforward logic: standard null-safety pattern
  • Low risk: non-breaking, preventive change only

Poem

🐰 A null check hops in with graceful care,
Guarding the code from exceptions rare,
When messages vanish into thin air,
Our safety net catches them there!

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 …" is directly related to the main change in the changeset. The raw summary confirms that the modification adds a null check for event.getMessage() to prevent NullPointerExceptions in the CONVERSATION_CHAT_COMPLETED branch. The title is specific about the problem being fixed and clearly identifies both the trigger condition (conversation.chat.completed event type) and the issue (NPE), making it understandable to someone reviewing the pull request history.
Description Check ✅ Passed The pull request description is clearly related to the changeset and provides specific, meaningful information about the bug being addressed. It explains both when the error occurs (when chat event type is conversation.chat.completed) and the exact error message that results from the null pointer issue. The description directly aligns with the code changes documented in the raw summary, which confirms the modification adds a null check to prevent this specific NullPointerException, making it neither vague nor generic.
✨ 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 (1)
example/src/main/java/example/chat/StreamChatExample.java (1)

57-59: Add null check to prevent NPE.

The same NPE risk that was fixed for CONVERSATION_CHAT_COMPLETED exists here. If event.getMessage() returns null for CONVERSATION_MESSAGE_DELTA events, this will throw a NullPointerException.

Apply this diff to add a null guard:

               if (ChatEventType.CONVERSATION_MESSAGE_DELTA.equals(event.getEvent())) {
-                System.out.print(event.getMessage().getContent());
+                if (event.getMessage() != null) {
+                  System.out.print(event.getMessage().getContent());
+                }
               }
🧹 Nitpick comments (2)
example/src/main/java/example/chat/StreamChatExample.java (2)

38-38: Remove stray semicolon.

There's an unnecessary semicolon after the builder pattern that should be removed for cleaner code.

             .build();
-    ;

79-81: Translate Chinese comments to English for consistency.

The comments are in Chinese while the rest of the codebase uses English. Consider translating for consistency.

-    // 为了防止程序立即退出,添加一个简单的等待
+    // 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)
🔇 Additional comments (2)
example/src/main/java/example/chat/StreamChatExample.java (2)

61-62: LGTM! Null check correctly prevents NPE.

The null guard properly addresses the reported NullPointerException when event.getMessage() returns null for CONVERSATION_CHAT_COMPLETED events. The logic flow is correct.


65-65: Add null checks before accessing event.getChat().getUsage().getTokenCount().

The method chain at line 65 lacks defensive null checks. Testing patterns in the codebase (e.g., WorkFlowRunServiceTest.java line 109) show developers explicitly verify getUsage() != null before accessing it, indicating these methods can return null. Consider:

if (event.getChat() != null && event.getChat().getUsage() != null) {
  System.out.println("Token usage:" + event.getChat().getUsage().getTokenCount());
}

@mifengjun
Copy link
Author

@CLAassistant add labels fix

@mifengjun mifengjun closed this Oct 29, 2025
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