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(HearingCasesAdapter.java:91)
        at com.abdulhalimkasim.mymention20.Adapters.HearingCasesAdapter.onBindViewHolder(HearingCasesAdapter.java:21)

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) {
            super(itemView);

            mFileRef = itemView.findViewById(R.id.fileref);
            mParties = itemView.findViewById(R.id.parties);
            mCourt = itemView.findViewById(R.id.court);
            mCourtAt = itemView.findViewById(R.id.courtat);
            mCaseNo = itemView.findViewById(R.id.caseno);
            mCaseStatus = itemView.findViewById(R.id.status);
            mNextDate = itemView.findViewById(R.id.date);
        }
    }



    public HearingCasesAdapter(ArrayList<CasesModel> hearingCasesList) {

        mHearingCasesList = hearingCasesList;
    }

    @Override
    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;
    }

    @Override
    public void onBindViewHolder(HearingCasesViewHolder holder, int position) {



        CasesModel currentItem = mHearingCasesList.get(position);

        holder.mFileRef.setText(currentItem.getFileRef());
        holder.mParties.setText(currentItem.getParties());
        holder.mCourt.setText(currentItem.getCourt());
        holder.mCourtAt.setText(currentItem.getCourtAt());
        holder.mCaseNo.setText(currentItem.getCaseNo());
        holder.mCaseStatus.setText(currentItem.getCaseStatus());

        // below is the codes with the problem as indicated
        // in Logcat (HearingCasesAdapter.java:91)above

        holder.mNextDate.setText(currentItem.getNextDate()); 



    }

    @Override
    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);


        holder.mNextDate.setText(dateTime);
``

or

holder.mNextDate.setText(currentItem.getNextDate().toString());

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