Error NPE when send message

Hi,

I got error NullPointerException when try to send message. I use Sendbird version 3.0.156.

Here is the stacktrace:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.sendbird.android.j1.f()' on a null object reference
   at com.happyfresh.happychat.ChatUser.<init>(ChatUser.java:20)
   at com.happyfresh.happychat.ChatMessage.setBaseMessage(ChatMessage.java:161)
   at com.happyfresh.happychat.ChatViewModel.sendMessage(ChatViewModel.java:398)
   at com.happyfresh.happychat.ChatFragment.lambda$onViewCreated$10(ChatFragment.java:225)
   at com.happyfresh.happychat.ChatFragment.lambda$onViewCreated$10$ChatFragment(ChatFragment.java)
   at com.happyfresh.happychat.-$$Lambda$ChatFragment$xx5VIYsa6-fWBmGhiuJkYgu73Mw.onSubmit(-.java:2)
   at com.happyfresh.happychat.widget.MessageInput.onSubmit(MessageInput.java:182)
   at com.happyfresh.happychat.widget.MessageInput.onClick(MessageInput.java:125)
   at android.view.View.performClick(View.java:6642)
   at android.view.View.performClickInternal(View.java:6619)
   at android.view.View.access$3100(View.java:790)
   at android.view.View$PerformClick.run(View.java:26189)
   at android.os.Handler.handleCallback(Handler.java:873)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:224)
   at android.app.ActivityThread.main(ActivityThread.java:7147)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:511)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)

Any suggestion how to fix the problem?
Thanks

Hi, @razibkani Could you give me the code snippet so that I can know what code I’m accessing?

Hi @LeoShin ,
sorry for late response, I had something todo and forgot to follow up this.

This is the snippet

if (baseMessage instanceof UserMessage) {
        UserMessage userMessage = (UserMessage) baseMessage;
        setRequestId(userMessage.getRequestId());
        setText(userMessage.getMessage());
        setUser(new ChatUser(userMessage.getSender()));
        setTranslations(userMessage.getTranslations());
        setDirectMessage(ChatContent.convertStringJsonToChatContent(userMessage.getData()));
    }
    else if (baseMessage instanceof FileMessage) {
        FileMessage fileMessage = (FileMessage) baseMessage;
        setRequestId(fileMessage.getRequestId());
        setImageUrl(fileMessage.getUrl());
        setUser(new ChatUser(fileMessage.getSender()));
    }
    else if (baseMessage instanceof AdminMessage) {
        AdminMessage adminMessage = (AdminMessage) baseMessage;
        setRequestId("0");
        setUser(new ChatUser());
        setText(adminMessage.getMessage());
    }

Based on StackTrace, I got null sender from BaseMessage. Do you have any idea why this happen?

What line is the error? It doesn’t seem to be a problem with the code.

@LeoShin This line

setUser(new ChatUser(userMessage.getSender()));

ChatUser need User object from Sendbird that received from userMessage.getSender().

Somehow got NPE when access getSender()

INSTANCEOF passing means that the instance exists, is this possible? :face_with_monocle:

I think the getSender is returning null. And is there any NPE in ChatUser?

This is ChatUser constructor:

public ChatUser(User user) {
    id = user.getUserId();
    name = user.getNickname();
}

so I think we get BaseMessage with null sender.

One think that I don’t know is why or what condition that make sender null

I assume the sender of that message is you (the current user), right?
The sender can be null in case the Sendbird.connect fails (offline mode, network issue) since we can’t retrieve the current user’s information. Could you check if the message is sent after a successful connect?