Мое приложение аварийно завершает работу и продолжает иметь эту ошибку java .lang.NullPointerException: Невозможно передать нуль для аргумента 'pathString' в child (), не могу посетить следующую страницу - PullRequest
0 голосов
/ 30 апреля 2020

Я пробовал много способов остановить взлом sh, но безрезультатно, ваша помощь будет высоко оценена. Это сообщение Logcat:

Процесс: com.niftyware.i_gbadunblog, PID: 16919 java .lang.NullPointerException: Невозможно передать нуль для аргумента 'pathString' в child () в com.google. firebase.database.DatabaseReference.child (com.google.firebase: firebase-database @@ 19.2.1: 96) на com.google.firebase.database.DataSnapshot.child (com.google.firebase: firebase-database @@ 19.2 .1: 65) на com.niftyware.i_gbadunblog.Adapters.CommentsAdapter $ CommentViewHolder $ 1.onDataChange (CommentsAdapter. java: 210) на com.google.firebase.database.core.ValueEventRegistration.fireEvent (com.google.firebase: firebase-database @@ 19.2.1: 75) на com.google.firebase.database.core.view.DataEvent.fire (com.google.firebase: firebase-database @@ 19.2.1: 63) на com.google. firebase.database.core.view.EventRaiser $ 1.run (com.google.firebase: firebase-database @@ 19.2.1: 55) в android .os.Handler.handleCallback (обработчик. java: 883)

Это код адаптера комментариев до части строки ошибки

 @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) {

                }
            });
        }

1 Ответ

0 голосов
/ 02 мая 2020

Ребята Я нашел решение в методе setLikeBtnCommentStatus, что ошибка исходила от ключа Comment, который содержался внутри оператора if. Я только что изменил

if (dataSnapshot.child (CommentKey) .hasChild (currentUserID)) {countLikes = (int) dataSnapshot.child (CommentKey) .getChildrenCount (); // Подсчитывает количество лайков в одном сообщении displayNoOfLikes.setText ((Integer.toString (countLikes) + ("Likes"))); }

Для if (dataSnapshot.child (String.valueOf (CommentKey)). HasChild (currentUserID)) {countLikes = (int) dataSnapshot.child (CommentKey) .getChildrenCount (); // Подсчитывает количество лайков в одном сообщении displayNoOfLikes.setText ((Integer.toString (countLikes) + ("Likes"))); } Это исправило мою проблему

...