Skip to content

Null pointer Exception on Recycler View On item click listener Activity Close when item got clicked

Hello I am new in Android development
I have tried different way to show data in RecyclerView from firebase. At first I set on click listener on item of RecyclerView and it work’s fine but After that I make some changes in code now I am trying to test it’s showing Null Exception pointer but I didn’t change anything on Adapter So why it’s showing Null pointer Exception can you please check Where I am mistaking. When I click on RecyclerView item Activity is automatically getting close.

see my Log cat


2020-10-29 16:15:14.953 21026-21026/? E/gamesdownloade: Unknown bits set in runtime_flags: 0x8000
2020-10-29 16:15:14.977 21026-21026/? E/libc: Access denied finding property "persist.sys.theme"
2020-10-29 16:15:15.041 21026-21055/? E/Perf: Fail to get file list com.downloadfrom.psp.gamesdownloader
2020-10-29 16:15:15.042 21026-21055/? E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-10-29 16:15:15.042 21026-21055/? E/Perf: Fail to get file list com.downloadfrom.psp.gamesdownloader
2020-10-29 16:15:15.042 21026-21055/? E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-10-29 16:15:24.376 21026-21026/com.downloadfrom.psp.gamesdownloader E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.downloadfrom.psp.gamesdownloader, PID: 21026
    java.lang.NullPointerException: Attempt to invoke interface method 'void com.downloadfrom.psp.gamesdownloader.ViewHolder$ClickListener.onItemClick(android.view.View, int)' on a null object reference
        at com.downloadfrom.psp.gamesdownloader.ViewHolder$1.onClick(
        at android.view.View.performClick(
        at android.view.View.performClickInternal(
        at android.view.View.access$3800(
        at android.view.View$
        at android.os.Handler.handleCallback(
        at android.os.Handler.dispatchMessage(
        at android.os.Looper.loop(
        at java.lang.reflect.Method.invoke(Native Method)

This is my View Holder/Adepter of Recycler view Check it please.


import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

public class ViewHolder extends RecyclerView.ViewHolder {

    View view;

    public ViewHolder( View itemView) {

        view = itemView;

        itemView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View view) {

                mClickListener.onItemLongClick(view, getAdapterPosition());

                return true;

    public void setDetails(Context ctx, String title, String image,String download,String size, String discription){

        TextView GameName;
        TextView Size,Download,Discription;
        ImageView GameImage;

        Size = view.findViewById(;
        Download = view.findViewById(;
        Discription = view.findViewById(;

        GameName = view.findViewById(;
        GameImage = view.findViewById(;



    private ViewHolder.ClickListener mClickListener;

    public interface ClickListener{

        void onItemClick(View view,int position);
        void onItemLongClick(View view,int position);

    public void setOnClickListener(ViewHolder.ClickListener clickListener){

        mClickListener = clickListener;



This is my MainActivity On start method In the this method I am showing data in Recycler View from firebase And Also set on click listener here check it please

//MainActivity On start Method

    protected void onStart() {
        FirebaseRecyclerAdapter<Games, ViewHolder> firebaseRecyclerAdapter =
                new FirebaseRecyclerAdapter<Games, ViewHolder>(
                ) {
                    protected void populateViewHolder(ViewHolder viewHolder, Games games, int i) {

                        viewHolder.setDetails(getApplicationContext(), games.getTitle(), games.getImage(),

                    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                        ViewHolder viewHolder = super.onCreateViewHolder(parent,viewType);

                        viewHolder.setOnClickListener(new ViewHolder.ClickListener() {
                            public void onItemClick(View view, int position) {

                                Toast.makeText(MainActivity.this,"You Clicked on this Game",Toast.LENGTH_SHORT).show();


                            public void onItemLongClick(View view, int position) {
                                Toast.makeText(MainActivity.this,"You Long Clicked on this Game",Toast.LENGTH_SHORT).show();


                        return super.onCreateViewHolder(parent, viewType);



And Also check Model class


public class Games {

    String title;
    String size;
    String image;
    String download;
    String discription;

    public Games() {

    public String getTitle() {
        return title;

    public void setTitle(String title) {
        this.title = title;

    public String getSize() {
        return size;

    public void setSize(String size) {
        this.size = size;

    public String getImage() {
        return image;

    public void setImage(String image) {
        this.image = image;

    public String getDownload() {
        return download;

    public void setDownload(String download) { = download;

    public String getDiscription() {
        return discription;

    public void setDiscription(String discription) {
        this.discription = discription;



Your ViewHolder is null inside the onCreateViewHolder method

Instead of using

ViewHolder viewHolder = super.onCreateViewHolder(parent,viewType);

Use this

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.your_xml_layout, parent, false);
return new ViewHolder(view);

and @Override onBindViewHolder method

protected void onBindViewHolder(ViewHolder holder, final int position, Model model) {
    holder.setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
              // your click logic

for ref check this link

User contributions licensed under: CC BY-SA
8 People found this is helpful