javaandroiddatepickerdialog

DatePickerDialog in Fragment Force Close


i need your help. I new in Android App and I want to create a datepicker with DatePickerDialog. I can get the dialog to show up and choose the date but when i clicked "OK" the app is closed. I dont know where i get things wrong, i tried to look for all the solution but i still can't solve this problem.

public class NotificationsFragment extends Fragment {

private NotificationsViewModel notificationsViewModel;
private DatePickerDialog datePickerDialog;
private Button lihatBtn;
private TextView tanggalDisplay, hariDisplay, bulanDisplay;
private CardView cardView;
private SimpleDateFormat simpleDateFormat;
private String hari, tanggal, bulan;
private DatePickerDialog.OnDateSetListener dateSetListener;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    notificationsViewModel =
            new ViewModelProvider(this).get(NotificationsViewModel.class);
    View root = inflater.inflate(R.layout.fragment_notifications, container, false);
    simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
    TextView hariDisplay = root.findViewById(R.id.text_hari_riwayat);
    TextView tanggalDisplay = root.findViewById(R.id.text_tanggal_riwayat);
    TextView bulanDisplay = root.findViewById(R.id.text_riwayat);
    CardView cardView = root.findViewById(R.id.card_date);
    Button lihatBtn = root.findViewById(R.id.lihat);

    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDateDialog();
        }
    });

    return root;
}

private void showDateDialog(){

    /**
     * Calendar untuk mendapatkan tanggal sekarang
     */
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    datePickerDialog = new DatePickerDialog(requireContext(), dateSetListener, year, month, day);

    datePickerDialog.getWindow();
    datePickerDialog.show();


    /**
     * Initiate DatePicker dialog
     */
    dateSetListener = new DatePickerDialog.OnDateSetListener(){
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            month = month + 1;
            bulan = month + " " + year;
            tanggal = String.valueOf(day);
            bulanDisplay.setText(bulan);
            tanggalDisplay.setText(tanggal);
        }
    };
  }
}

This is the XML

    <androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/card_date"
        android:layout_marginTop="4dp">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/text_tanggal_riwayat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:textAlignment="textStart"
                android:textSize="30sp"
                android:textColor="@color/primary"
                android:textStyle="bold"
                android:text="27"
                />
            <TextView
                android:id="@+id/text_hari_riwayat"
                android:layout_toEndOf="@+id/text_tanggal_riwayat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:textAlignment="textStart"
                android:textStyle="bold"
                android:textColor="@color/black"
                android:textSize="15sp"
                android:text="Sabtu"
                />
            <TextView
                android:id="@+id/text_bulan_riwayat"
                android:layout_toEndOf="@+id/text_tanggal_riwayat"
                android:layout_below="@+id/text_hari_riwayat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:textAlignment="textStart"
                android:textStyle="bold"
                android:textColor="@color/black"
                android:textSize="15sp"
                android:text="November 2020"
                />
        </RelativeLayout>
    </androidx.cardview.widget.CardView>

This is the error log

2020-11-29 14:22:57.243 25192-25192/com.github.otoponik E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.github.otoponik, PID: 25192
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at com.github.otoponik.ui.notifications.NotificationsFragment$2.onDateSet(NotificationsFragment.java:112)
    at android.app.DatePickerDialog.onClick(DatePickerDialog.java:173)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:228)
    at android.app.ActivityThread.main(ActivityThread.java:7782)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)

Solution

  • You must instantiate dateSetListener before using it :

     private void showDateDialog(){
    
     dateSetListener = new DatePickerDialog.OnDateSetListener(){
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                month = month + 1;
                bulan = month + " " + year;
                tanggal = String.valueOf(day);
                bulanDisplay.setText(bulan);
                tanggalDisplay.setText(tanggal);
            }
        };
        /**
         * Calendar untuk mendapatkan tanggal sekarang
         */
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        datePickerDialog = new DatePickerDialog(requireContext(), dateSetListener, year, month, day);
    
        datePickerDialog.getWindow();
        datePickerDialog.show();
    
    
        /**
         * Initiate DatePicker dialog
         */
       
      }