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 before accessing event.getMessage() in the CONVERSATION_CHAT_COMPLETED event handler to prevent potential NullPointerException. The condition now verifies that the message is non-null alongside the MessageType.FOLLOW_UP check, maintaining existing control flow behavior.

Changes

Cohort / File(s) Summary
Null-safety improvement
example/src/main/java/example/chat/StreamChatExample.java
Added null-check before accessing event.getMessage() in the CONVERSATION_CHAT_COMPLETED branch alongside existing MessageType.FOLLOW_UP condition to prevent NullPointerException

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

  • Single focused change with clear defensive programming intent
  • Minimal scope affecting only one conditional branch
  • Low risk of unintended side effects

Possibly related PRs

Poem

🐰 A null-check hops into view,
Guarding messages tried and true,
No more crashes in the stream,
Safety's now the rabbit's dream! ✨

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 title "fix: When chat event type is conversation.chat.completed, a NPE will …" is directly and fully related to the changeset. It clearly identifies the specific scenario (chat event type is conversation.chat.completed), the problem being addressed (NullPointerException/NPE), and the nature of the fix. The raw summary confirms the change adds a null-check in the CONVERSATION_CHAT_COMPLETED branch to prevent this exact NPE. The title is specific, descriptive, and accurately represents the primary change without vague language.
Description Check ✅ Passed The description is directly related to the changeset and provides relevant context for the fix. It clearly states the problem: a NullPointerException is thrown when processing chat events with type conversation.chat.completed, and provides the specific error message indicating that getMessage() returns null. This aligns perfectly with the fix implemented, which adds a null-check before accessing event.getMessage(). The description is sufficiently detailed to explain the issue without being overly verbose.
✨ 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: 1

🧹 Nitpick comments (2)
example/src/main/java/example/chat/StreamChatExample.java (2)

38-38: Remove the extra semicolon.

There's an unnecessary semicolon after the build() statement.

-            .build();
-    ;
+            .build();

78-83: Consider translating comments to English for consistency.

The comments on lines 78-80 are in Chinese, while all other comments in the file are in English. For codebase consistency and broader team accessibility, consider translating them.

-    // 为了防止程序立即退出,添加一个简单的等待
+    // 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 2d20b95.

📒 Files selected for processing (1)
  • example/src/main/java/example/chat/StreamChatExample.java (1 hunks)
🧰 Additional context used
🪛 GitHub Actions: CI
example/src/main/java/example/chat/StreamChatExample.java

[error] 58-58: spotless check failed. The file has formatting violations detected by the Spotless Maven Plugin. Run 'mvn spotless:apply' to fix code style issues in this file.

🔇 Additional comments (1)
example/src/main/java/example/chat/StreamChatExample.java (1)

57-59: Review comment is based on incorrect assumption about null-safety for different event types.

The review suggests adding a null-check to CONVERSATION_MESSAGE_DELTA similar to CONVERSATION_CHAT_COMPLETED. However, examination of the PR and codebase shows:

  • The PR specifically addresses null-safety for CONVERSATION_CHAT_COMPLETED events only (line 60-62)
  • All four example files in the codebase (StreamChatExample.java, ChatWithImageExample.java, SubmitToolOutputExample.java, StreamWorkflowChatExample.java) consistently use event.getMessage().getContent() without null-checks for CONVERSATION_MESSAGE_DELTA events
  • The API documentation indicates CONVERSATION_MESSAGE_DELTA is for "incremental message...usually when type=answer", suggesting the message field is guaranteed to be present

The null-safety issue addressed in the PR appears specific to CONVERSATION_CHAT_COMPLETED events, not CONVERSATION_MESSAGE_DELTA. Adding defensive checks to CONVERSATION_MESSAGE_DELTA is therefore unnecessary and would diverge from the established pattern across all examples.

Likely an incorrect or invalid review comment.

}
if (ChatEventType.CONVERSATION_CHAT_COMPLETED.equals(event.getEvent())) {
if (MessageType.FOLLOW_UP.equals(event.getMessage().getType())) {
if (event.getMessage() != null && MessageType.FOLLOW_UP.equals(event.getMessage().getType())) {
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Correct NPE fix, but address the formatting failure.

The null-check correctly prevents the NullPointerException described in the PR. The logic properly handles both cases: when a message exists with type FOLLOW_UP, and when the message is null (falling through to print token usage).

However, the pipeline indicates a Spotless formatting violation. Run mvn spotless:apply to fix the code style.

🤖 Prompt for AI Agents
In example/src/main/java/example/chat/StreamChatExample.java around line 61 the
null-check for event.getMessage() fixes the NPE but the file fails Spotless
formatting; run mvn spotless:apply (or your project's formatter) and commit the
formatted changes so the formatting violation is resolved.

@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