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 base
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());
}
});
}
}