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.