0

Trying to Delete messages in a chatting application. There are 2 ways, 1) Delete for Me and 2) Delete for Everyone. But I'm not able to perform any of those as I'm facing an error in Firebase database which causes the app to crash.

I'm getting this ERROR :

Process: com.example.flashchat, PID: 23248

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.flashchat.Adapters.MessagesAdapter.lambda$onBindViewHolder$0$MessagesAdapter(MessagesAdapter.java:110)

MessagesAdapter.java:

viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
        public boolean onLongClick(View v) {
            View view = LayoutInflater.from(context).inflate(R.layout.delete_dialog, null);
            DeleteDialogBinding binding = DeleteDialogBinding.bind(view);
            AlertDialog dialog = new AlertDialog.Builder(context)
                    .setTitle("Delete Message")
                    .setView(binding.getRoot())
                    .create();

            binding.everyone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    message.setMessage("This message is removed.");
                    message.setFeeling(-1);
                    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);
                    dialog.dismiss();
                }
            });

            binding.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FirebaseDatabase.getInstance().getReference()
                            .child("chats")
                            .child(senderRoom)
                            .child("messages")
                            .child(message.getMessageId()).setValue(null);
                    dialog.dismiss();
                }
            });

            binding.cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.show();

            return false;
        }
    });

Image of the Database

Error Description Image

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Related: *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)*. It includes a link to *"[How do I format my code blocks?](https://meta.stackoverflow.com/questions/251361/)"* – Peter Mortensen Mar 04 '22 at 19:48
  • 1
    The error message indicates that one of the values you are passing to `child(...)` is null. You'll have to check each value to determine which one it is. – Frank van Puffelen Mar 04 '22 at 21:43
  • Are you sure you aren't passing non-null values to the `.child()` method? – Alex Mamo Mar 05 '22 at 09:54
  • I'm sure about not passing non-null values, but I'm still not able to figure out the error @AlexMamo. – Navi Vohera Mar 05 '22 at 10:26
  • I've never seen this error message be wrong, so when it says "Can't pass null for argument 'pathString' in child()" I am inclined to believe that you are passing `null` in a call to `child(...)`. You can check that in a debugger, log the values, or put an `if()` around each of the statements that might be causing the problem. Following the guidance in here: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Mar 05 '22 at 15:07

0 Answers0