Click here to Skip to main content
15,355,050 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing an application in which I am using recycler view. When I am trying to display recycler view from a fragment I am getting this error 'Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at Adapters.PostAdapter.getItemCount'

Why is this error occurring and how can I fix this?

What I have tried:

My HomeFragment.java Code:
Java
package Fragments;

import android.os.Bundle;

import androidx.annotation.ContentView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.smart.instagram.R;

import java.util.ArrayList;
import java.util.List;

import Adapters.PostAdapter;
import Models.PostsModel;

public class HomeFragment extends Fragment {
    RecyclerView story_rv;
    RecyclerView posts_rv;
    LinearLayoutManager layoutManager;
    List<PostsModel> postsList;
    PostAdapter postAdapter;



    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

       View parentHolder = inflater.inflate(R.layout.fragment_home, container, false);

//
//        //posts rv
//
        return parentHolder;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        story_rv = getView().findViewById(R.id.story_rv);
        posts_rv = getView().findViewById(R.id.post_rv);
        postAdapter = new PostAdapter(postsList);
        layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        posts_rv.setLayoutManager(layoutManager);
        posts_rv.setAdapter(postAdapter);

        postsList = new ArrayList<>();
        postsList.add(new PostsModel(R.drawable.story1, "Elisa Shermon", R.drawable.ic_baseline_more_vert_24, R.drawable.story1
                , R.drawable.ic_baseline_favorite_border_24, "2.1K", R.drawable.ic_baseline_chat_bubble_24, "230",
                R.drawable.ic_baseline_send_24, "100", R.drawable.ic_baseline_bookmark_border_24));
        postsList.add(new PostsModel(R.drawable.story2, "Justin Christian", R.drawable.ic_baseline_more_vert_24, R.drawable.story2
                , R.drawable.ic_baseline_favorite_border_24, "9.6K", R.drawable.ic_baseline_chat_bubble_24, "890",
                R.drawable.ic_baseline_send_24, "409", R.drawable.ic_baseline_bookmark_border_24));
        postsList.add(new PostsModel(R.drawable.story3, "Travel Bug", R.drawable.ic_baseline_more_vert_24, R.drawable.story3
                , R.drawable.ic_baseline_favorite_border_24, "2.1M", R.drawable.ic_baseline_chat_bubble_24, "230K",
                R.drawable.ic_baseline_send_24, "900K", R.drawable.ic_baseline_bookmark_border_24));
        postsList.add(new PostsModel(R.drawable.story4, "Nick Jonas", R.drawable.ic_baseline_more_vert_24, R.drawable.story4
                , R.drawable.ic_baseline_favorite_border_24, "4M", R.drawable.ic_baseline_chat_bubble_24, "450K",
                R.drawable.ic_baseline_send_24, "453", R.drawable.ic_baseline_bookmark_border_24));
        postsList.add(new PostsModel(R.drawable.story5, "The Gaming Beast", R.drawable.ic_baseline_more_vert_24, R.drawable.story5
                , R.drawable.ic_baseline_favorite_border_24, "2.1K", R.drawable.ic_baseline_chat_bubble_24, "230",
                R.drawable.ic_baseline_send_24, "100", R.drawable.ic_baseline_bookmark_border_24));
        postsList.add(new PostsModel(R.drawable.story6, "Food Spot", R.drawable.ic_baseline_more_vert_24, R.drawable.story1
                , R.drawable.ic_baseline_favorite_border_24, "2.1K", R.drawable.ic_baseline_chat_bubble_24, "230",
                R.drawable.ic_baseline_send_24, "100", R.drawable.ic_baseline_bookmark_border_24));
    }
}


My PostAdapter Code:-
Java
package Adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.smart.instagram.R;

import java.util.List;

import Models.PostsModel;

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

    public List<PostsModel> postList;

    public PostAdapter(List<PostsModel>postList){
        this.postList = postList;
    }

    @NonNull
    @Override
    //Implements design of our layout resource file
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.posts_rv, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int profile_pic = postList.get(position).getProfile_pic();
        int more = postList.get(position).getMore();
        int post_image = postList.get(position).getPost_image();
        int like = postList.get(position).getLike();
        int share = postList.get(position).getShare();
        int comment = postList.get(position).getComment();
        int bookmark = postList.get(position).getBookmark();
        String user_name = postList.get(position).getUser_name();
        String like_no = postList.get(position).getLike_no();
        String share_no = postList.get(position).getShare_no();
        String comment_no = postList.get(position).getComment_no();

        holder.setData(profile_pic, more, post_image, like, share, comment, bookmark,
                user_name, like_no, comment_no, share_no);

    }

    //Helps to bind data from MainActivity to post_rv


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


    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView profile_img, post_img, more_img, like_img, comment_img, share_img, bookmark_img;
        private TextView name, like_nos, comment_nos, share_nos;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            profile_img = itemView.findViewById(R.id.profile_pic);
            name = itemView.findViewById(R.id.user_name);
            more_img = itemView.findViewById(R.id.more);
            post_img = itemView.findViewById(R.id.post_image);
            like_img = itemView.findViewById(R.id.like);
            like_nos = itemView.findViewById(R.id.like_no);
            share_img = itemView.findViewById(R.id.share);
            share_nos = itemView.findViewById(R.id.share_no);
            comment_img = itemView.findViewById(R.id.comment);
            comment_nos = itemView.findViewById(R.id.comment_no);
            bookmark_img = itemView.findViewById(R.id.bookmark);

        }

        public void setData(int profile_pic, int more, int post_image, int like, int share, int comment, int bookmark,
                            String user_name, String like_no, String comment_no, String share_no) {
            profile_img.setImageResource(profile_pic);
            more_img.setImageResource(more);
            post_img.setImageResource(post_image);
            like_img.setImageResource(like);
            share_img.setImageResource(share);
            comment_img.setImageResource(comment);
            bookmark_img.setImageResource(bookmark);
            name.setText(user_name);
            like_nos.setText(like_no);
            comment_nos.setText(comment_no);
            share_nos.setText(share_no);
        }
    }


}
Posted
Updated 17-Jun-21 1:14am

1 solution

Java
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    story_rv = getView().findViewById(R.id.story_rv);
    posts_rv = getView().findViewById(R.id.post_rv);
    postAdapter = new PostAdapter(postsList); // postsList has not been initialised yet.

You pass an uninitialised object to the PostAdapter constructor. So when you call postAdapter.getItemCount() it throws the error, because the reference is null. You must initialise the list before you create the adapter.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900