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;
}
}