Home > front end >  my chat application crashes when ever in try to react on a message
my chat application crashes when ever in try to react on a message

Time:05-22

When ever I try to send messages from device to device the app works fine a ,But when ever i try to react in a message the app crashes and returns back to main activity page, i don't know what is causing the error

here is the messageAdapter.java code :

package com.example.baatcheet.Adapters;


import com.bumptech.glide.Glide;
import com.example.baatcheet.Models.Message;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.baatcheet.R;
import com.example.baatcheet.databinding.ItemReceiveBinding;
import com.example.baatcheet.databinding.ItemSentBinding;
import com.github.pgreze.reactions.ReactionPopup;
import com.github.pgreze.reactions.ReactionsConfig;
import com.github.pgreze.reactions.ReactionsConfigBuilder;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;


import java.util.ArrayList;


public class MessagesAdapter extends RecyclerView.Adapter {

    Context context;
    ArrayList<Message> messages;
    final int ITEM_SENT  = 1;
    final int ITEM_RECEIVE = 2;

    String senderRoom;
    String receiverRoom;

    public MessagesAdapter(Context context , ArrayList<Message> messages , String senderRoom , String receiverRoom){
        this.context = context;
        this.messages = messages;
        this.senderRoom = senderRoom;
        this.receiverRoom = receiverRoom;


    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType == ITEM_SENT){
            View view = LayoutInflater.from(context).inflate(R.layout.item_sent, parent, false);
            return  new SentViewHolder(view);
        }else{
            View view = LayoutInflater.from(context).inflate(R.layout.item_receive, parent, false);
            return  new ReceiverViewHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        Message message = messages.get(position);
        if(FirebaseAuth.getInstance().getUid().equals(message.getSenderId())){
            return ITEM_SENT;
        }else{
            return ITEM_RECEIVE;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Message message = messages.get(position);

        int[] reactions =  new int[]{
                R.drawable.ic_fb_like,
                R.drawable.ic_fb_love,
                R.drawable.ic_fb_laugh,
                R.drawable.ic_fb_wow,
                R.drawable.ic_fb_sad,
                R.drawable.ic_fb_angry
        };
        ReactionsConfig config = new ReactionsConfigBuilder(context)
                .withReactions(reactions)
                .build();

        ReactionPopup popup = new ReactionPopup(context, config, (pos) -> {

            if(holder.getClass() == SentViewHolder.class){
                SentViewHolder viewHolder = (SentViewHolder)holder;
                if(pos >= 0 && pos <= 6) {
                    viewHolder.binding.feeling.setImageResource(reactions[pos]);
                    viewHolder.binding.feeling.setVisibility(View.VISIBLE);
                }
            }else {
                ReceiverViewHolder viewHolder = (ReceiverViewHolder) holder;
                if (pos >= 0 && pos <= 6) {
                    viewHolder.binding.feeling.setImageResource(reactions[pos]);
                    viewHolder.binding.feeling.setVisibility(View.VISIBLE);
                }
            }

            message.setFeeling(pos);
            FirebaseDatabase.getInstance().getReference()
                    .child("chats")
                    .child(senderRoom)
                    .child("messages")
                    .child(message.getMessageId())
                    .setValue(message);

            FirebaseDatabase.getInstance().getReference()
                    .child("chats")
                    .child(receiverRoom)
                    .child("messages")
                    .child(message.getMessageId())
                    .setValue(message);



            return true; // true is closing popup, false is requesting a new selection
        });

        if(holder.getClass() == SentViewHolder.class){
            SentViewHolder viewHolder = (SentViewHolder)holder;



                    viewHolder.binding.message.setText(message.getMessage());

                    if(message.getFeeling() >= 0){
//                        message.setFeeling(reactions[(int)message.getFeeling()]);
                        viewHolder.binding.feeling.setImageResource(reactions[message.getFeeling()]);
                        viewHolder.binding.feeling.setVisibility(View.VISIBLE);
                    }
                    else{
                        viewHolder.binding.feeling.setVisibility(View.GONE);
                    }
                    viewHolder.binding.message.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            popup.onTouch(v,event);
                            return false;
                        }
                    });
        }else{
            ReceiverViewHolder viewHolder = (ReceiverViewHolder) holder;
            viewHolder.binding.message.setText(message.getMessage());

            if(message.getFeeling() >= 0){
//                message.setFeeling(reactions[(int)message.getFeeling()]);
                viewHolder.binding.feeling.setImageResource(reactions[message.getFeeling()]);
                viewHolder.binding.feeling.setVisibility(View.VISIBLE);

            }
            else{
                viewHolder.binding.feeling.setVisibility(View.GONE);
            }

            viewHolder.binding.message.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    popup.onTouch(v,event);
                    return false;
                }
            });

        }
    }

    @Override
    public int getItemCount() {

        return messages.size();
    }

    public class SentViewHolder extends RecyclerView.ViewHolder{

        ItemSentBinding binding;
        public SentViewHolder(@NonNull View itemView) {
            super(itemView);
            binding  = ItemSentBinding.bind(itemView);
        }

    }
    public class ReceiverViewHolder extends RecyclerView.ViewHolder{
        ItemReceiveBinding binding;
        public ReceiverViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = ItemReceiveBinding.bind(itemView);
        }
    }
}

Here is the getter and setter class for above adapter class , messgaes.java :

package com.example.baatcheet.Models;

public class Message {
    private String messageId , message , senderId;
    private long timeStamp;
    private int feeling = -1;
    public String imageUrl;


    public Message() {

    }

    public Message(String message, String senderId, long timeStamp) {
        this.message = message;
        this.senderId = senderId;
        this.timeStamp = timeStamp;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setSenderId(String senderId) {
        this.senderId = senderId;
    }

    public void setTimeStamp(long timeStamp) {
        this.timeStamp = timeStamp;
    }

    public void setFeeling(int feeling) {
        this.feeling = feeling;

    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getMessageId() {
        return messageId;
    }

    public String getMessage() {
        return message;
    }

    public String getSenderId() {
        return senderId;
    }

    public long getTimeStamp() {
        return timeStamp;
    }

    public int getFeeling() {
        return feeling;
    }

    
}

I dont actually know where my app is getting crashed

the logcat shows the following error when ever i try to react on a message

[samsung-sm_m305f-52001052ca7b863f]: E/InputEventReceiver: Exception dispatching input event.
[samsung-sm_m305f-52001052ca7b863f]: E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
[samsung-sm_m305f-52001052ca7b863f]: E/MessageQueue-JNI: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
        at com.google.firebase.database.DatabaseReference.child(DatabaseReference.java:96)
        at com.example.baatcheet.Adapters.MessagesAdapter.lambda$onBindViewHolder$0$com-example-baatcheet-Adapters-MessagesAdapter(MessagesAdapter.java:104)
        at com.example.baatcheet.Adapters.MessagesAdapter$$ExternalSyntheticLambda0.invoke(Unknown Source:10)
        at com.github.pgreze.reactions.ReactionViewGroup.onTouchEvent(ReactionViewGroup.kt:247)
        at android.view.View.dispatchTouchEvent(View.java:14376)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3857)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3863)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3551)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3863)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3551)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3863)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3551)
        at android.widget.PopupWindow$PopupDecorView.dispatchTouchEvent(PopupWindow.java:2850)
        at android.view.View.dispatchPointerEvent(View.java:14644)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6486)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:6273)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5711)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5764)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5730)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5886)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5738)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5943)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5711)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5764)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5730)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5738)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5711)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8870)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8731)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8684)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:9006)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:194)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:336)
        at android.os.Looper.loop(Looper.java:197)
        at android.app.ActivityThread.main(ActivityThread.java:7807)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
[samsung-sm_m305f-52001052ca7b863f]: D/AndroidRuntime: Shutting down VM
[samsung-sm_m305f-52001052ca7b863f]: E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.baatcheet, PID: 28114
    java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
        at com.google.firebase.database.DatabaseReference.child(DatabaseReference.java:96)
        at com.example.baatcheet.Adapters.MessagesAdapter.lambda$onBindViewHolder$0$com-example-baatcheet-Adapters-MessagesAdapter(MessagesAdapter.java:104)
        at com.example.baatcheet.Adapters.MessagesAdapter$$ExternalSyntheticLambda0.invoke(Unknown Source:10)
        at com.github.pgreze.reactions.ReactionViewGroup.onTouchEvent(ReactionViewGroup.kt:247)
        at android.view.View.dispatchTouchEvent(View.java:14376)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3857)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3535)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3863)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3551)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3863)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3551)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3863)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3551)
        at android.widget.PopupWindow$PopupDecorView.dispatchTouchEvent(PopupWindow.java:2850)
        at android.view.View.dispatchPointerEvent(View.java:14644)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6486)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:6273)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5711)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5764)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5730)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5886)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5738)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5943)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5711)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5764)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5730)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5738)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5711)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8870)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8731)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8684)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:9006)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:194)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:336)
        at android.os.Looper.loop(Looper.java:197)
        at android.app.ActivityThread.main(ActivityThread.java:7807)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)

CodePudding user response:

You have a NPE for the argument pathString but I cannot find it in your code. You should add a null-check to the relevant part and see if it works.

E/MessageQueue-JNI: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
  • Related