How to use the setText() in the onBindViewHolder() in a RecyclerView adapter class to display date of Date type from Realm database

by halim   Last Updated June 13, 2019 08:26 AM

I have an activity which has a recycler view. In this RecyclerView, I've set a cardView to display the data which I will state below within the codes. One of the data is of type Date. I have no issue in saving the date from a calendar dialog where I've converted the string from the calendar into a date type. I know this since the console for the Add New Case activity confirmed the data was saved and I am able to display one of the fields of the data entered in the All Cases List activity.

However, when I want to display the data in the activity using the adapter which I have the app will crash.

The logcat shows: FATAL EXCEPTION: main

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.util.Date.toString()' on a null object reference
        at com.abdulhalimkasim.mymention20.Adapters.HearingCasesAdapter.onBindViewHolder(
        at com.abdulhalimkasim.mymention20.Adapters.HearingCasesAdapter.onBindViewHolder(

This is weird since all data was indeed saved into the realm database. Data is all there.

Below are the codes for the onBindViewHolder in the HearingCasesAdapter class:

public class HearingCasesAdapter extends RecyclerView.Adapter<HearingCasesAdapter.HearingCasesViewHolder> {

    private ArrayList<CasesModel> mHearingCasesList;

    public static class HearingCasesViewHolder extends RecyclerView.ViewHolder{

        public TextView mFileRef;
        public TextView mParties;
        public TextView mCourt;
        public TextView mCourtAt;
        public TextView mCaseNo;
        public TextView mCaseStatus;
        public TextView mNextDate;

        public HearingCasesViewHolder(View itemView) {

            mFileRef = itemView.findViewById(;
            mParties = itemView.findViewById(;
            mCourt = itemView.findViewById(;
            mCourtAt = itemView.findViewById(;
            mCaseNo = itemView.findViewById(;
            mCaseStatus = itemView.findViewById(;
            mNextDate = itemView.findViewById(;

    public HearingCasesAdapter(ArrayList<CasesModel> hearingCasesList) {

        mHearingCasesList = hearingCasesList;

    public HearingCasesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.hearing_cases_cardview, parent, false);
        HearingCasesViewHolder hcvh = new HearingCasesViewHolder(v);
        return hcvh;

    public void onBindViewHolder(HearingCasesViewHolder holder, int position) {

        CasesModel currentItem = mHearingCasesList.get(position);


        // below is the codes with the problem as indicated
        // in Logcat (



    public int getItemCount() {

        return mHearingCasesList.size();

have tried converting the date to string first using the codes below but it is still not working.

        Date date = currentItem.getNextDate();

        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

        String dateTime = dateFormat.format(date);




Please help me with some suggestion on how to go about it. Thank you in advance.
Tags : android realm

Related Questions

Updated November 28, 2018 11:26 AM

Updated August 07, 2017 10:26 AM

Updated September 12, 2017 08:26 AM

Updated May 09, 2017 16:26 PM