Ich stoß auf ein Problem, nämlich ein Error (aus dem Logcat):
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:248)
Der Error entsteht durch getReceiver()
. Nur ein Teil des Codes ist hier vorhanden:
// check edit text change listener
messageEt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().trim().length() == 0 ) {
checkTypingStatus("noOne");
} else {
checkTypingStatus(hisUid); // uid of receiver
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
readMessages();
seenMessage();
}
private void seenMessage() {
userRefForSeen = FirebaseDatabase.getInstance().getReference("Chats");
seenListener = userRefForSeen.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
ModelChat chat = ds.getValue(ModelChat.class);
if ( chat.getReceiver().equals(myUid) && chat.getSender().equals(hisUid)) {
HashMap<String, Object> hasSeenHashMap = new HashMap<>();
hasSeenHashMap.put("isSeen", true);
ds.getRef().updateChildren(hasSeenHashMap);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
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.getReceiver().equals(hisUid) && chat.getSender().equals(myUid))) {
chatList.add(chat);
}
// adapter
adapterChat = new AdapterChat(ChatActivity.this, chatList, hisImage);
adapterChat.notifyDataSetChanged();
// set adapter to recyclerview
recyclerView.setAdapter(adapterChat);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public class ModelChat {
String message, receiver, sender, timestamp;
boolean isSeen;
public ModelChat() {
}
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 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;
}