1

Currently developing a Forum App where user who post question will get a notification if someone respond to them. Right now the coding has logical error where the notification does not go into the original poster attribute in the User table inside Firebase

Below is the code to add notification

private void addToHisNotification(String hisUid,String Post_Key,String notification){
    String timestamp = ""+System.currentTimeMillis();

    HashMap<Object, String> hashMap = new HashMap<>();
    hashMap.put("timestamp",timestamp);
    hashMap.put("Postkey",Post_Key);
    hashMap.put("uid",hisUid);
    hashMap.put("notification",notification);
    hashMap.put("sUid",current_user_id);

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");
    ref.child(hisUid).child("Notifications").child(timestamp).setValue(hashMap)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {

                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });
}

And this is the function to add Answer and will call function Notification

    PostAnsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Userref.child(current_user_id).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()){
                        String userName = dataSnapshot.child("Username").getValue().toString();
                        String profileImg = dataSnapshot.child("profileImage").getValue().toString();
                        String department = dataSnapshot.child("Department").getValue().toString();
                        ValidateAnswer(userName,profileImg, department);
                        addToHisNotification("" + hisUid,"" + Post_Key, "Commented on your post");

                        AnswerInputText.setText("");
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

        }
    });

The Null in the image is supposed to be the original poster UID but instead it becomes null. How do I solve this so that the Null becomes the UID of the OP?

This is the image of the database.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Zaphyrzan
  • 11
  • 2
  • 1
    You say *instead it becomes null*. How do you check that? – Alex Mamo Dec 16 '21 at 12:18
  • Where is `hisUid` initialized? – bradley101 Dec 16 '21 at 15:36
  • From the output in your database, it seems obvious that `hisUid` is `null`. None of the code you shared initialized `hisUid`, so that *could* be the reason that it is `null`. On the other hand, you could also be initializing `hisUid` in some code you're not showing us. Either way: this is a variant of a `NullPointerException`, so the steps to troubleshoot it are https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Dec 16 '21 at 15:38
  • Where and how should I initialized hisUid? – Zaphyrzan Dec 17 '21 at 03:05

0 Answers0