NullPointerException in ChatActivity.onPrepareOptionsMenu when currentConversation is not yet loaded

Open Beginner friendly
#6,123 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
android, kotlin
Domain
mobile-dev

Research direction

Start in ChatActivity.kt at onPrepareOptionsMenu, especially line 2731, and inspect the existing nullable currentConversation handling nearby. Reproduce the issue with the described instrumented navigation or automated test setup. Done means opening ChatActivity while the conversation is still loading no longer throws a NullPointerException and the unified-search menu visibility remains correct.

Written by the indexing model from the issue text.

Description

1. to develop

Summary

ChatActivity crashes with NullPointerException in onPrepareOptionsMenu when the menu is prepared before currentConversation is fully loaded. The crash is reliably reproducible on master by opening any conversation in an automated test environment (instrumented test, faster than human navigation).

Reproduction

  1. Build current master (tested at HEAD 5aeace618 bump version to 23.0.0).
  2. Launch ChatActivity programmatically (e.g. from an instrumented test, or by navigating from the conversation list very quickly).
  3. The activity crashes before fully rendering.

Stack trace

java.lang.NullPointerException
    at com.nextcloud.talk.chat.ChatActivity.onPrepareOptionsMenu(ChatActivity.kt:2731)
    at android.app.Activity.onPreparePanel(Activity.java:4480)
    at androidx.activity.ComponentActivity.onPreparePanel(ComponentActivity.kt:464)
    at androidx.appcompat.view.WindowCallbackWrapper.onPreparePanel(WindowCallbackWrapper.java:99)
    at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.onPreparePanel(...)
    at androidx.appcompat.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:459)
    at androidx.appcompat.app.ToolbarActionBar$1.run(ToolbarActionBar.java:58)

Root cause

ChatActivity.kt:2731 uses a non-null assertion on currentConversation:

searchItem.isVisible =
    hasSpreedFeatureCapability(spreedCapabilities, SpreedFeatures.UNIFIED_SEARCH) &&
    currentConversation!!.remoteServer.isNullOrEmpty() &&  // ← !! crashes when currentConversation is null
    !isChatThread()

onPrepareOptionsMenu is called by the system as soon as the toolbar is drawn, but the conversation is loaded asynchronously. Under fast/automated navigation, the menu is prepared before currentConversation is set.

Suggested fix

Either guard the whole block with currentConversation?.let { ... }, or use safe-call on the field:

searchItem.isVisible =
    hasSpreedFeatureCapability(spreedCapabilities, SpreedFeatures.UNIFIED_SEARCH) &&
    (currentConversation?.remoteServer?.isNullOrEmpty() ?: true) &&
    !isChatThread()

(Several other currentConversation?.… calls already exist in the same method, suggesting this !! is an oversight rather than an invariant.)

Suspected commit

Likely introduced by 96b4f4308 New chat architecture + replace ChatKit with Compose + chat relay via signaling, which restructured ChatActivity.

Environment

  • talk-android master @ 5aeace618
  • Android 13 / API 33 emulator (Pixel 3a, headless)
  • Reproduced via instrumented androidx.test UI navigation
Dominant language
Kotlin
Stars
739
Forks
321
Avg merge
17h 28m
Merged PRs (30d)
152

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nextcloud/talk-android

All issues in nextcloud/talk-android

Similar issues

More Kotlin issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.