0

I tried many methods to stop the crash but no avail, your help would be highly appreciated. This is the Logcat message:

Process: com.niftyware.i_gbadunblog, PID: 16919 java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() at com.google.firebase.database.DatabaseReference.child(com.google.firebase:firebase-database@@19.2.1:96) at com.google.firebase.database.DataSnapshot.child(com.google.firebase:firebase-database@@19.2.1:65) at com.niftyware.i_gbadunblog.Adapters.CommentsAdapter$CommentViewHolder$1.onDataChange(CommentsAdapter.java:210) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.1:75) at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.1:63) at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.1:55) at android.os.Handler.handleCallback(Handler.java:883)

This is the Comments Adapter code up to the part of the line of the error

 @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {

        final String CommentKey = mData.get(position).getCommentKey();

        Glide.with(mCtx).load(mData.get(position).getUimg()).into(holder.img_user);
        holder.txt_name.setText(mData.get(position).getUname());
        holder.txt_content.setText(mData.get(position).getContent());
        holder.txt_date.setText(timestampToString((Long) mData.get(position).getTimestamp()));


        holder.reply.setText(mData.get(position).getReply());



        holder.setLikeCommentBtnStatus(CommentKey);

        holder.setUnlikeCommentBtnStatus(CommentKey);

        holder.setReplyCommentBtnStatus();

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

                // Here I would add a code of a variable which would check the number of likes and to change the color of the liked post
                likesChecker = true;
                // User can likes a single post one time only 1 count
                likesRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if(likesChecker.equals(true)){
                            if (dataSnapshot.child(CommentKey).hasChild(currentUserIDX)){// if likes exists
                                likesRef.child(CommentKey).child(currentUserIDX).removeValue();
                                likesChecker = false;
                            }
                            else{
                                likesRef.child(CommentKey).child(currentUserIDX).setValue(true);
                                likesChecker = false;
                            }
                        }
                    }

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

                    }
                });

            }
        });

        holder.btnDislikeComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dislikesChecker = false;

                disLikesRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if(dislikesChecker.equals(true)){
                            if(dataSnapshot.child(CommentKey).hasChild(currentUserIDX)){
                                disLikesRef.child(CommentKey).child(currentUserIDX).removeValue();
                                dislikesChecker = false;
                            }
                            else{
                                disLikesRef.child(CommentKey).child(currentUserIDX).setValue(true);
                                dislikesChecker = false;
                            }
                        }
                    }

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

                    }
                });
            }
        });

    }

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

    public  class CommentViewHolder extends RecyclerView.ViewHolder {

        ImageView img_user;
        TextView txt_name, txt_content, txt_date;
        ImageButton btnLikeComment, btnDislikeComment;


        TextView displayNoOfLikes, displayNoOfDislikes, reply;


        int countLikes;
        int countDislikes;
        String currentUserID;
        DatabaseReference LikeRef, DislikeRef, ReplyRef;

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

            img_user = itemView.findViewById(R.id.comment_user_img);
            txt_name = itemView.findViewById(R.id.comment_username);
            txt_content = itemView.findViewById(R.id.comment_content);
            txt_date = itemView.findViewById(R.id.comment_date);

            LikeRef = FirebaseDatabase.getInstance().getReference().child("Likes");
            DislikeRef = FirebaseDatabase.getInstance().getReference().child("Dislikes");
            ReplyRef = FirebaseDatabase.getInstance().getReference().child("Replies");



            btnLikeComment = itemView.findViewById(R.id.comment_like_post);
            btnLikeComment.setBackgroundColor(Color.TRANSPARENT);

            btnDislikeComment = itemView.findViewById(R.id.comment_dislike_post);
            btnDislikeComment.setBackgroundColor(Color.TRANSPARENT);


            displayNoOfLikes = itemView.findViewById(R.id.comment_like_post_display_no_of_likes);
            displayNoOfLikes = itemView.findViewById(R.id.comment_like_post_display_no_of_dislikes);
            reply = itemView.findViewById(R.id.comment_reply);




        }

        public void setLikeCommentBtnStatus(final String CommentKey){
            LikeRef.addValueEventListener(new ValueEventListener() {

                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    if(dataSnapshot.child(CommentKey).hasChild(currentUserID)){
                        countLikes = (int) dataSnapshot.child(CommentKey).getChildrenCount(); //Counts the number of likes in a single post
                        displayNoOfLikes.setText((Integer.toString(countLikes) + (" Likes")));
                    }
                    else{
                        // If the user unlikes the post I have to update the number of likes and change the color of the like
                        countLikes = (int) dataSnapshot.child(CommentKey).getChildrenCount(); //Counts the number of likes in a single post
                        displayNoOfLikes.setText((Integer.toString(countLikes) + (" Likes")));
                    }
                }

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

                }
            });
        }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
jaxbriggs9
  • 27
  • 6
  • You have to provide, `screenShot for database`, `the POJO class` , `the code where yu send the data`. – Hasan Bou Taam Apr 30 '20 at 12:07
  • From the error message and code you shared, it seems that `CommentKey` or `currentUserIDX` is null or empty. Use the guidance in this question to help you troubleshoot further: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Apr 30 '20 at 13:43
  • @Hasan Bou Taam, do you mean the entire fireBase database?, I do not understand what you meant by P0j0 class or the code where you sent the data. Please could you be more elaborate. Thank you – jaxbriggs9 Apr 30 '20 at 16:30
  • @Frank van Puffelen I checked I already initialised them both. I have gone through those questions they do not seem to help me I would appreciate if you reopen back the post so I could get help to solve this. Thanks – jaxbriggs9 Apr 30 '20 at 16:33
  • As said: with the information you provides so far, there's nothing better we can do than that highly upvoted answer for the exact same error. If you think your problem is different, make sure your question contains the [minimal complete/standalone code and data with which any of us can reproduce the problem](http://stackoverflow.com/help/mcve). Once you do, it will become eligible for reopening. – Frank van Puffelen Apr 30 '20 at 16:40
  • I finally found the answer to my question you have to reopen it to help people in the future who might have this kind of issue, it wasn't on that link you sent me. @Frank van Pufflen – jaxbriggs9 May 01 '20 at 14:08

1 Answers1

0

Guys I found the solution on the setLikeBtnCommentStatus method the error was coming from the Comment key which was contained inside the if statement. I just changed

if(dataSnapshot.child(CommentKey).hasChild(currentUserID)){ countLikes = (int) dataSnapshot.child(CommentKey).getChildrenCount(); //Counts the number of likes in a single post displayNoOfLikes.setText((Integer.toString(countLikes) + (" Likes"))); }

To if(dataSnapshot.child(String.valueOf(CommentKey)).hasChild(currentUserID)){ countLikes = (int) dataSnapshot.child(CommentKey).getChildrenCount(); //Counts the number of likes in a single post displayNoOfLikes.setText((Integer.toString(countLikes) + (" Likes"))); } That fixed my issue

jaxbriggs9
  • 27
  • 6