0

I received the error

"java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at com.example.socialapp.ChatActivity$5.onDataChange(ChatActivity.java:255)"

and I've been searching other similar issues other people have had. I'm not sure what is showing as null, when I look at the Firebase database, I see uid fields with data in it.

I would like to fix this issue and I would also like to know why this is happening.

This is a snippet of my ChatActivity, my readMessages function, where the error is occurring

 private void readMessages() {
     chatList = new ArrayList<>();
     DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("Chats");
     dbRef.addValueEventListener(new ValueEventListener() {
         @Override
         public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            chatList.clear();
            for (DataSnapshot ds: dataSnapshot.getChildren()){
                ModelChat chat = ds.getValue(ModelChat.class);
                if(chat.getReceiver().equals(myUid) && chat.getSender().equals(theirUid) ||
                        chat.getReceiver().equals(theirUid) && chat.getSender().equals(myUid)){
                    chatList.add(chat);
                }

                //Adapter
                adapterChat = new AdapterChat(ChatActivity.this, chatList, theirImage);
                adapterChat.notifyDataSetChanged();
                //Set adapter to RecyclerView
                recyclerView.setAdapter(adapterChat);
            }
         }

The error is at line 255

if(chat.getReceiver().equals(myUid) && chat.getSender().equals(theirUid) ||
                        chat.getReceiver().equals(theirUid) && chat.getSender().equals(myUid)){
                    chatList.add(chat);
                }

Any help would be so much appreciated! Thank you

Edit: Here's my ModelChat.java file

package com.example.socialapp.models;

public class ModelChat {

 String message, receiver, sender, timestamp;
 boolean isSeen;

 public ModelChat(String message, String receiver, String sender, String timestamp, boolean isSeen) {
 this.message = message;
 this.receiver = receiver;
 this.sender = sender;
 this.timestamp = timestamp;
 this.isSeen = isSeen;
 }

 public ModelChat() {

 }

 public String getMessage() {
 return message;
 }

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

 public String getReceiver() {
 return receiver;
 }

 public void setReceiver(String receiver) {
 this.receiver = receiver;
 }

 public String getSender() {
 return sender;
 }

 public void setSender(String sender) {
 this.sender = sender;
 }

 public String getTimestamp() {
 return timestamp;
 }

 public void setTimestamp(String timestamp) {
 this.timestamp = timestamp;
 }

 public boolean isSeen() {
 return isSeen;
 }

 public void setSeen(boolean seen) {
 isSeen = seen;
 }
}
Indigo
  • 167
  • 1
  • 2
  • 13
  • `chat.getReceiver()` is returning null. You're going to have to debug your application to figure out why this is. – Doug Stevenson Jan 31 '20 at 20:39
  • This means that either `getReceiver()` or `getSender()` is returning `null`. Typically this is because the data in `ds` is not what you think it is, or is not complete. 1) Can you edit your question to include the data at `dbRef` (as text, no screenshots). You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). 2) It would also be useful to see what your `ModelChat` looks like. – Frank van Puffelen Jan 31 '20 at 20:39
  • @FrankvanPuffelen So I just looked in Firebase and you were right! The few messages I've sent, show just sender uid's and there is no receiver node to hold their uid's. But I will get that info up here and the ModelChat also. Thanks! – Indigo Jan 31 '20 at 20:43

0 Answers0