0

I'm using the following code in the activity ApplyLeave. I would like to pick the date and save the date, month and year to public int variables.

            final Calendar calendar = Calendar.getInstance();
            DatePickerDialog dialog = new DatePickerDialog(ApplyLeave.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker arg0, int year, int month, int day_of_month) {
                    calendar.set(Calendar.YEAR, year);
                    calendar.set(Calendar.MONTH, (month)); //add +1 for next month calendar
                    calendar.set(Calendar.DAY_OF_MONTH, day_of_month);
                    String myFormat = "dd/MM/yyyy";
                    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.getDefault());
                    dtTmp = sdf.format(calendar.getTime());

                }
            },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
            dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());//used to hide previous date,month and year
            calendar.add(Calendar.YEAR, 0);
            //dialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());//used to hide future date,month and year
            dialog.show();
            String[] selTmp = dtTmp.split("/");

            selBdate = Integer.parseInt(selTmp[0]);
            selBmonth = Integer.parseInt(selTmp[1]);
            selByear = Integer.parseInt(selTmp[2]);
            stDt = MyUtility.getJulianDay(selByear, selBmonth, selBdate, ApplyLeave.this);
            eBegin.setText(dtTmp);

I get the error

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] java.lang.String.split(java.lang.String)' on a null object reference at Line "String[] selTmp = dtTmp.split("/");

If I comment this line and the three following, the date gets picked up, but need to click twice to get the correct date set.

The datepicker code is taken from Answer by @Sunil.

Vipin Chouhan
  • 120
  • 10
Sriram Nadiminti
  • 107
  • 2
  • 12
  • `dtTmp` is null because you have written it synchronously . Better move it inside `onDateSet`.. – ADM Aug 20 '20 at 06:39
  • I have moved all the four lines and placed after dtTmp declaration. Still it is null. Now it reads dtTmp = sdf.format(calendar.getTime()); String[] selTmp = dtTmp.split("/"); selBdate = Integer.parseInt(selTmp[0]); selBmonth = Integer.parseInt(selTmp[1]); selByear = Integer.parseInt(selTmp[2]); – Sriram Nadiminti Aug 20 '20 at 06:45

2 Answers2

1

try this :

public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener{
        TextView textView;
        Button txt_open_date_picker;
        int yy,mm,dd;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            textView = findViewById(R.id.text);
            txt_open_date_picker = (Button) findViewById(R.id.txt_open_date_picker);
    
            txt_open_date_picker.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DatePickerDialog datePickerDialog = new DatePickerDialog(
                            MainActivity.this,
                            MainActivity.this,
                            Calendar.getInstance().get(Calendar.YEAR),
                            Calendar.getInstance().get(Calendar.MONTH),
                            Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
    
                    datePickerDialog.show();
                }
            });
    
        }
    
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            String date = dayOfMonth + "/" + month + "/" + year;
            textView.setText(date);
        }
    }
0

try this it's working for me

     final Calendar c = Calendar.getInstance();
                    int mYear = c.get(Calendar.YEAR);
                    int mMonth = c.get(Calendar.MONTH);
                    int mDay = c.get(Calendar.DAY_OF_MONTH);
                    
                    DatePickerDialog datePickerDialog = new DatePickerDialog(Activity_five.this,
                            new DatePickerDialog.OnDateSetListener() {
    
                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int monthOfYear, int dayOfMonth) {
    
                                    Date date = new Date(year, monthOfYear, dayOfMonth);
    
                                    Calendar calendar = Calendar.getInstance();
                                    calendar.set(year, monthOfYear, dayOfMonth);
                                    @SuppressLint("SimpleDateFormat")
                                    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
                                    // String cdate = formatter.format(date);
                                    String strDate = formatter.format(calendar.getTime());
    
                                     
                                }
                            }, mYear, mMonth, mDay);
                    
//for set max value in date
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
                    datePickerDialog.show();
  • Where should I keep this code - within the onclicklistener? or in Oncreate section. Pl guide. When I kept it in the OnClickListener, I have to click twice to get the date set. First time I'm not getting the date to this variable strDate. – Sriram Nadiminti Aug 20 '20 at 06:59