taskrabbit / react-native-zendesk-chat

React Native Wrapper around Zendesk Chat v2
MIT License
130 stars 128 forks source link

Fatal Exception: java.lang.NullPointerException #120

Open MakChan opened 3 years ago

MakChan commented 3 years ago
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void zendesk.chat.ObservationScope.cancel()' on a null object reference
       at com.taskrabbit.zendesk.RNZendeskChatModule$1.update(RNZendeskChatModule.java:369)
       at com.taskrabbit.zendesk.RNZendeskChatModule$1.update(RNZendeskChatModule.java:363)
       at zendesk.chat.ObservableData.notifyObservers(ObservableData.java:175)
       at zendesk.chat.ObservableData.setData(ObservableData.java:161)
       at zendesk.chat.ZendeskChatProvider$2.update(ZendeskChatProvider.java:176)
       at zendesk.chat.ZendeskChatProvider$2.update(ZendeskChatProvider.java:173)
       at zendesk.chat.MainThreadPoster$1$1.run(MainThreadPoster.java:43)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:246)
       at android.app.ActivityThread.main(ActivityThread.java:8512)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

Device: Samsung Galaxy S20+ (SM-G985F) Android 11

Versions: react-native 0.64.2 react-native-zendesk-chat 0.4.1

thuynt99 commented 3 years ago

@MakChan Have any solution?

pt7892 commented 2 years ago

This is reproducible with following steps:

  1. start chat
  2. close pre chat form
  3. start chat again
  4. finish entering form details
  5. start chat with agent
  6. crash happens

This is happening because observeChatState is called every time startChat is called, even if chat state observer was attached before

I created this patch file react-native-zendesk-chat+0.4.1.patch as temporary solution, tested it and seems to fix the issue

diff --git a/node_modules/react-native-zendesk-chat/android/src/main/java/com/taskrabbit/zendesk/RNZendeskChatModule.java b/node_modules/react-native-zendesk-chat/android/src/main/java/com/taskrabbit/zendesk/RNZendeskChatModule.java
index 143efb9..bb2717f 100644
--- a/node_modules/react-native-zendesk-chat/android/src/main/java/com/taskrabbit/zendesk/RNZendeskChatModule.java
+++ b/node_modules/react-native-zendesk-chat/android/src/main/java/com/taskrabbit/zendesk/RNZendeskChatModule.java
@@ -358,6 +358,11 @@ public class RNZendeskChatModule extends ReactContextBaseJavaModule {

     // https://support.zendesk.com/hc/en-us/articles/360055343673
     public void setupChatStartObserverToSetVisitorInfo(){
+        // Stop if we already have chat state observer
+        if (observationScope != null) {
+          return;
+        }
+
         // Create a temporary observation scope until the chat is started.
         observationScope = new ObservationScope();
         Chat.INSTANCE.providers().chatProvider().observeChatState(observationScope, new Observer<ChatState>() {
@@ -366,8 +371,10 @@ public class RNZendeskChatModule extends ReactContextBaseJavaModule {
                 ChatSessionStatus chatStatus = chatState.getChatSessionStatus();
                 // Status achieved after the PreChatForm is completed
                 if (chatStatus == ChatSessionStatus.STARTED) {
-                    observationScope.cancel(); // Once the chat is started disable the observation
-                    observationScope = null; // Clean things up to avoid confusion.
+                    if (observationScope != null) {
+                        observationScope.cancel(); // Once the chat is started disable the observation
+                        observationScope = null; // Clean things up to avoid confusion.
+                    }
                     if (pendingVisitorInfo == null) { return; }

                     // Update the information MID chat here. All info but Department can be updated