-2

I'm an android newbie...

This is my code:

final Button taskDateButton = (Button)findViewById(R.id.taskTimeButton);
taskDateButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        DatePickerDialog datePickerDialog = new DatePickerDialog(TaskActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                taskDateCalendar.set(Calendar.YEAR, year);
                taskDateCalendar.set(Calendar.MONTH, monthOfYear);
                taskDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                taskDate = taskDateCalendar.getTime();
                setDateButtonsWithDates();
                TimePickerDialog timePickerDialog = new TimePickerDialog(TaskActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        taskDateCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        taskDateCalendar.set(Calendar.MINUTE, minute);
                        taskDate = taskDateCalendar.getTime();
                        setDateButtonsWithDates();
                    }
                }, taskDateCalendar.get(Calendar.HOUR_OF_DAY), taskDateCalendar.get(Calendar.MINUTE), false);
                timePickerDialog.show();

            }
        }, taskDateCalendar.get(Calendar.YEAR), taskDateCalendar.get(Calendar.MONTH), taskDateCalendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();


    }
});

It seems to be crashing when loading DatePickerDialog, specifically on this line:

DatePickerDialog datePickerDialog = new DatePickerDialog(TaskActivity.this, new DatePickerDialog.OnDateSetListener() {

I looked around but couldn't see what's causing this.

This is the error I get:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.Calendar.get(int)' on a null object reference

Any help?

Thanks

Nimrod Shai
  • 1,149
  • 13
  • 26
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Knossos May 12 '15 at 07:43

2 Answers2

2

your taskDateCalendar is never initialized.

try to add taskDateCalendar = Calendar.getInstance(); before using it.

Dragondraikk
  • 1,659
  • 11
  • 21
2

Your taskDateCalendar object seems to be null. Call taskDateCalendar = Calendar.getInstance() before your taskDateCalendar.set(Calendar.YEAR, year).

Ircover
  • 2,406
  • 2
  • 22
  • 42