Error NPE when send message


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' on a null object reference
   at com.happyfresh.happychat.ChatUser.<init>(
   at com.happyfresh.happychat.ChatMessage.setBaseMessage(
   at com.happyfresh.happychat.ChatViewModel.sendMessage(
   at com.happyfresh.happychat.ChatFragment.lambda$onViewCreated$10(
   at com.happyfresh.happychat.ChatFragment.lambda$onViewCreated$10$ChatFragment(
   at com.happyfresh.happychat.-$$Lambda$ChatFragment$xx5VIYsa6-fWBmGhiuJkYgu73Mw.onSubmit(
   at com.happyfresh.happychat.widget.MessageInput.onSubmit(
   at com.happyfresh.happychat.widget.MessageInput.onClick(
   at android.view.View.performClick(
   at android.view.View.performClickInternal(
   at android.view.View.access$3100(
   at android.view.View$
   at android.os.Handler.handleCallback(
   at android.os.Handler.dispatchMessage(
   at android.os.Looper.loop(
   at java.lang.reflect.Method.invoke(

Any suggestion how to fix the problem?

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;
        setUser(new ChatUser(userMessage.getSender()));
    else if (baseMessage instanceof FileMessage) {
        FileMessage fileMessage = (FileMessage) baseMessage;
        setUser(new ChatUser(fileMessage.getSender()));
    else if (baseMessage instanceof AdminMessage) {
        AdminMessage adminMessage = (AdminMessage) baseMessage;
        setUser(new ChatUser());

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?