I am creating a notepad application, however, this error occurs at

startup:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference. 
In the log, the program points to these lines:at com.example.tasktrackingandmanagment.adapters.MyNoteAdapter$ViewHolder.setNote(MyNoteAdapter.java:68)
                                                                                                    at com.example.tasktrackingandmanagment.adapters.MyNoteAdapter.onBindViewHolder(MyNoteAdapter.java:36)
                                                                                                    at com.example.tasktrackingandmanagment.adapters.MyNoteAdapter.onBindViewHolder(MyNoteAdapter.java:20)

Below is the code:

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

    List<MyNoteEntities> noteEntitiesList;

    public MyNoteAdapter(List<MyNoteEntities> noteEntitiesList) {
        this.noteEntitiesList = noteEntitiesList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.setNote(noteEntitiesList.get(position));

    }

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

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView title,textNote,dateTime;
        private LinearLayout linearLayout;
        RoundedImageView roundedImageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            title = itemView.findViewById(R.id.textTitle);
            textNote = itemView.findViewById(R.id.textNote);
            linearLayout = itemView.findViewById(R.id.layoutNote);
            roundedImageView = itemView.findViewById(R.id.imageNote_item);
        }

        public void setNote(MyNoteEntities noteEntities) {
            title.setText(noteEntities.getTitle());
            textNote.setText(noteEntities.getNoteText());
            dateTime.setText(noteEntities.getDateTime());

            GradientDrawable gradientDrawable = (GradientDrawable) linearLayout.getBackground();
            if (noteEntities.getColor() != null) {
                gradientDrawable.setColor(Color.parseColor(noteEntities.getColor()));
            }
            else{
                gradientDrawable.setColor(Color.parseColor("#FF937B"));
            }
        }
    }
}

0

There are 0 best solutions below