-1

Good day everyone! I have a problem the app crash and it has an error

java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
        at com.google.firebase.database.DatabaseReference.child(Unknown Source:6)
        at com.example.jasper.happy_paws.CommentAdapter.getUserInfo(CommentAdapter.java:95)
        at com.example.jasper.happy_paws.CommentAdapter.onBindViewHolder(CommentAdapter.java:51)
        at com.example.jasper.happy_paws.CommentAdapter.onBindViewHolder(CommentAdapter.java:26)
        at android.support.v7.widget.RecyclerView$Adapter

I dont see anyproblem with my adapter here is my code

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder> {

    private Context mContext;
    private List<CommentModel>mComment;
    private FirebaseUser firebaseUser;

    public CommentAdapter(Context mContext, List<CommentModel> mComment) {
        this.mContext = mContext;
        this.mComment = mComment;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.comment_items, parent, false);
        return new CommentAdapter.ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        final CommentModel commentModel = mComment.get(i);

        holder.comment.setText(commentModel.getComment());
        getUserInfo(holder.image_profile,holder.username, commentModel.getUserid());

        holder.comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, Account.class);
                intent.putExtra("userid", commentModel.getUserid());
                mContext.startActivity(intent);
            }
        });

        holder.image_profile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, Account.class);
                intent.putExtra("userid", commentModel.getUserid());
                mContext.startActivity(intent);
            }
        });



    }

    @Override
    public int getItemCount() {
        return mComment.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        public ImageView image_profile;
        public TextView username,comment;

        public ViewHolder(View itemView) {
            super(itemView);

            image_profile = itemView.findViewById(R.id.image_profile);
            username = itemView.findViewById(R.id.username);
            comment = itemView.findViewById(R.id.comment);
        }
    }
    private void getUserInfo(final ImageView imageView, final TextView username, String userid)
    {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("User").child(userid);
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                UsersHappyPaws usersHappyPaws = dataSnapshot.getValue(UsersHappyPaws.class);
                Glide.with(mContext).load(usersHappyPaws.getProfilepic()).into(imageView);
               username.setText(usersHappyPaws.getName());
            }



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

            }
        });
    }
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163

1 Answers1

0

Try this way.. If getting data code is working then pl change this things.

get data from firebase take into activity or fragment.

    private List<UsersHappyPaws> pawsList=new ArrayList<>();
private void getUserInfo(final ImageView imageView, final TextView username, String userid)
{
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("User").child(userid);
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            UsersHappyPaws usersHappyPaws = dataSnapshot.getValue(UsersHappyPaws.class);
            pawsList.add(usersHappyPaws);
        }



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

        }
    });
}

make adapter class make constructor add pass data into mange.take one array list into adapter class.

public CommentAdapter(Context mContext, List<CommentModel> mComment,List<UsersHappyPaws> pawsList) {
    this.mContext = mContext;
    this.mComment = mComment;
    this.pawsList=pawsList;
}

after that make object for UsersHappyPaws class and bind data according.

  • commentModelList = new ArrayList<>(); commentAdapter = new CommentAdapter(this,commentModelList,); recyclerView.setAdapter(commentAdapter); why i have error in this thank you Android team Godbless! – Jasper Ortiz Nov 20 '18 at 08:05
  • yes thats way to pass your data into adapter. –  Nov 20 '18 at 08:06
  • I have an error in this line commentAdapter = new CommentAdapter(this,commentModelList); sorry I'm a newbie – Jasper Ortiz Nov 20 '18 at 08:09
  • check commentAdapter parematers –  Nov 20 '18 at 08:10
  • thank you it worked Additional question in my other activity when i change the username or other datas of user it is working but it turns to black for a while and the logcat said You cannot start a load for a destroyed activity – Jasper Ortiz Nov 20 '18 at 09:12