-1

I'm working on a Recycler View, I set everything ok (as it seams ) but always the activity crashes, after making some debugging, I found where it occur, but never figure it out! Here is the my RecyclerView Adapter

enter code here

public class StudentsAdapter extends RecyclerView.Adapter<StudentsAdapter.mHolder> {
    private  List<Student> mStudents;
    private  Context mContext;

    public StudentsAdapter(List<Student> students, Context mContext) {
        Log.d("StudentsAdapter", "in Constructor");
        this.mStudents = students;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public mHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.d("StudentsAdapter", "in onCreateViewHolder");
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.note_list_students, parent, false);
        return new mHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull mHolder holder, int position) {
        Log.d("StudentsAdapter", "in onBindViewHolder");
        Student student = mStudents.get(position);
        Log.d("StudentsAdapter", "in getting Student Object");
        holder.nameTv.setText(student.getName());
        Log.d("StudentsAdapter", "in Setting Text name");
        holder.genderTv.setText(student.getGender());
        holder.birthdayTv.setText(student.getBirthday());
    }

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

    public class mHolder extends RecyclerView.ViewHolder {
        TextView nameTv, genderTv, birthdayTv;

        public mHolder(@NonNull View itemView) {
            super(itemView);
            Log.d("StudentsAdapter", "in View Holder");
            nameTv = itemView.findViewById(R.id.name);
            genderTv = itemView.findViewById(R.id.gender);
            birthdayTv = itemView.findViewById(R.id.birthday);

        }
    }
}

After making debug process, here is what I got:

2019-12-30 13:05:08.583 6169-6169/com.example.schooltendancemanager D/StudentsAdapter: in Constructor
2019-12-30 13:05:08.625 6169-6169/com.example.schooltendancemanager D/StudentsAdapter: in onCreateViewHolder
2019-12-30 13:05:08.638 6169-6169/com.example.schooltendancemanager D/StudentsAdapter: in View Holder
2019-12-30 13:05:08.638 6169-6169/com.example.schooltendancemanager D/StudentsAdapter: in onBindViewHolder
2019-12-30 13:05:08.638 6169-6169/com.example.schooltendancemanager D/StudentsAdapter: in getting Student Object
2019-12-30 13:05:08.642 6169-6169/com.example.schooltendancemanager E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.schooltendancemanager, PID: 6169
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.schooltendancemanager.ui.StudentsAdapter.onBindViewHolder(StudentsAdapter.java:43)
        at com.example.schooltendancemanager.ui.StudentsAdapter.onBindViewHolder(StudentsAdapter.java:18)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)



Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
omarb1989
  • 111
  • 5
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Phantômaxx Dec 30 '19 at 14:35

3 Answers3

1

Check to make sure your R.layout.note_list_students layout file contains Textviews with those id's.

1

I think you used a wrong id in this below line, please check in the design file note_list_students.xml if you have this id (gender)

genderTv = itemView.findViewById(R.id.gender);
R. A.
  • 192
  • 12
0

the problem caused by the Layout note_list_students, even if the TextViews are matched but I used to have two versions of Layout, Standard one which is note_list_students.xml and the other version note_list_students.xml v21, so I recreated it and used the standard Layout only!

Thank you for your response!

omarb1989
  • 111
  • 5