I have a root node named "Posts" in Firebase Realtime Database. Inside that, I have two nodes called "ImagePosts" and "TextPosts". And inside "ImagePosts" (and "TextPosts"), I have postIds of various posts. And inside a postID, I have all the details of that particular post. I want to get to these Post details. But if I use: Post post = mPosts.get(position) in my BindViewHolder inside PostAdapter, it gives me a Null Pointer Exception whenever I try to access any post detail using post object (Post is the name of the model).
Please click here to see database structure
For example, in the following code, I want to match post.getPostedBy() to userID (stored in a separate root node named "Users"), but it gives error: "Can't pass null for argument 'pathString' in child()" at line ".child(post.getPostedBy)".
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Post post = mPosts.get(position);
FirebaseDatabase.getInstance().getReference()
.child("Users")
.child(post.getPostedBy())
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
User user = snapshot.getValue(User.class);
Picasso.get().load(user.getprofilepic())
.placeholder(R.drawable.placeholder_user)
.into(holder.binding.profileImage);
Picasso.get().load(user.getprofilepic())
.placeholder(R.drawable.placeholder_user)
.into(holder.binding.profileImg);
holder.binding.userName.setText(user.getName());
holder.binding.profession.setText(user.getProfession());
holder.binding.postText.setText(Html.fromHtml("<b>"+user.getUsername() +"</b>" + " " + post.getPostDescription()));
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});