0

I get this java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() error when trying to add a comment to Firebase, I have tried many methods online but none seem to be of help, I have been stuck for days here. This is my data baseenter image description here

I am trying to add all comments inside an individual post when I try that I get the null pointer pathString() error, which I can't seem to figure out the cause. Below is my code.

public class PostDetailActivity extends AppCompatActivity {
   String  postId,

       @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the id of the post using intent(The issue seems to be from the postId)
        Intent intent = getIntent();
        postId = intent.getStringExtra("postId");

   private void postComment() {
        btnAddComment.setVisibility(View.INVISIBLE);

        String comment = editTextComment.getText().toString().trim();
        //Validate
        if(TextUtils.isEmpty(comment)){
            // Nothing was entered
            Toast.makeText(PostDetailsActivity.this, "Comment is empty...", Toast.LENGTH_SHORT).show();
            btnAddComment.setVisibility(View.VISIBLE);
            return;
        }


        String timeStamp = String.valueOf(System.currentTimeMillis());

        // Every individual post would have a child called "comments" that would contain comments of the post
        DatabaseReference commentReference = FirebaseDatabase.getInstance().getReference("Posts").child(postId).child("Comments"); 

        HashMap<String, Object> hashMap = new HashMap<>(); //insert info into the hashMap
        hashMap.put("commentID", timeStamp);
        hashMap.put("comment", comment);
        hashMap.put("timestamp", timeStamp);
        hashMap.put("userID", firebaseUser.getUid());
        hashMap.put("uEmail", firebaseUser.getEmail());
        hashMap.put("uName", firebaseUser.getDisplayName());
        hashMap.put("userImage", myDP);


        //Put the data into the database
        commentReference.child(timeStamp).setValue(hashMap).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                showMessage("Comment added");
                editTextComment.setText("");
                btnAddComment.setVisibility(View.VISIBLE);

                updateCommentCount();

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                //Failed
                showMessage("Failed to add comment : "+ e.getMessage());
            }
        });



    }

 }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
jaxbriggs9
  • 27
  • 6

1 Answers1

0

According to the error message you're passing a null value to the child() method, which is not allowed. In the code you shared, these are the lines that call child():

DatabaseReference commentReference = FirebaseDatabase.getInstance().getReference("Posts").child(postId).child("Comments"); 

...

commentReference.child(timeStamp).setValue(hashMap).addOnSuccessListener(new OnSuccessListener<Void>() {

Based on the error message and this code, it seems that either postId or timeStamp is null.

You can most easily detect which one it is by either placing a breakpoint on both lines and running the app in a debugger, or by logging the variables in the line just above where you use them.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • The line with the child() error is the one containing postId which is the first one amongst those you highlighed, I checked it I do not seem to find a where I haven't yet instantiated the postId or do not know if they way I did it with the intent method is right, I am confused over this. – jaxbriggs9 Jul 13 '20 at 16:06