Null Pointer Excemption on hard challenge. Please help!


#1

I am stuck since a couple of days to the hard challenge. I made up a ChoiceFragment class containing a Dialog implementation to menage the choice. Still, once i pick an option (i.e. date or time) I face a Null Pointer Excemption in ChoiceFragment. java //i made a comment to highlight the exception error.
here is CrimeFragment.java:

package com.bignerdranch.criminalintent;

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.support.v4.app.FragmentManager;

public class CrimeFragment extends Fragment {
	public static final String EXTRA_CRIME_ID = "com.bignerdranch.criminalintent.crime_id";
	String EXTRA_CHOICE = "com.bignerdranch.criminalintent.crime_choice";
	public static final String  DIALOG_CHOICE="choice";
	public static final int REQUEST_CHOICE=0;
	public static final int REQUEST_DATE=1;
	int mChoice;
	Crime mCrime;
	private EditText mTitleField;
	private  Button mDateButton;
	private CheckBox mSolvedCheckBox;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		UUID crimeId=(UUID)getArguments().getSerializable(EXTRA_CRIME_ID);
		mChoice=(int)getArguments().getInt(EXTRA_CHOICE);
		mCrime=new Crime();
		ChoiceFragment choicefragment;
		mCrime=CrimeLab.get(getActivity()).getCrime(crimeId);
	}
	public static CrimeFragment newInstance(UUID CrimeId){
		Bundle args=new Bundle();
		args.putSerializable(EXTRA_CRIME_ID, CrimeId);
		CrimeFragment fragment=new CrimeFragment();
		//attaches arguments to fragment after the fragment is created and before fragment is added to activity
		fragment.setArguments(args); 
		return fragment;
	}
	public void updateDate(){
		mDateButton.setText(mCrime.getDate().toString());
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle onSavedInstanceState){
		View v=inflater.inflate(R.layout.fragment_crime,  parent, false);
		mTitleField=(EditText)v.findViewById(R.id.crime_title);
		mTitleField.addTextChangedListener(new TextWatcher(){

		@Override
		public void afterTextChanged(Editable c) {}

		@Override
		public void beforeTextChanged(CharSequence c, int start, int count,
				int after) {}

		@Override
		public void onTextChanged(CharSequence c, int start, int before,
				int count) {
			mCrime.setTitle(c.toString());
		}
	});
	mDateButton=(Button)v.findViewById(R.id.crime_date);
	updateDate();
	mDateButton.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
		FragmentManager fm=getActivity().getSupportFragmentManager();
		ChoiceFragment dialog=new ChoiceFragment().newInstance(EXTRA_CHOICE);
		dialog.setTargetFragment(CrimeFragment.this,REQUEST_CHOICE);
		dialog.show(fm, DIALOG_CHOICE);
		}
	});
	mSolvedCheckBox=(CheckBox)v.findViewById(R.id.crime_solved);
	mSolvedCheckBox.setChecked(mCrime.isSolved());
	mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			mCrime.setSolved(isChecked);
			
		}
		
	});
	return v;
	}
public void onActivityResult(int requestCode, int resultCode,Intent data){
	if (resultCode!=Activity.RESULT_OK) return;
	if (requestCode==REQUEST_CHOICE){
		int  choice=data.getSerializableExtra(ChoiceFragment.EXTRA_CHOICE).hashCode();
	}
	
}
}
	

ChoiceFragment.java:

package com.bignerdranch.criminalintent;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.*;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.*;


public class ChoiceFragment extends DialogFragment {
	public static final String EXTRA_CHOICE = "com.bignerdranch.android.criminalintent.choice";
	public static final String DIALOG_DATE="date";
	public static final String DIALOG_TIME="time";
	public static final int REQUEST_CHOICE=0;
	TextView mDlgQuestionTextView;
	Crime mCrime;
	int mChoice;//0-date, 1-time
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		
		View v = getActivity().getLayoutInflater()
				.inflate(R.layout.dialog_choice, null);
		mDlgQuestionTextView=(TextView)v.findViewById(R.id.dialog_question);
		
		
		return new AlertDialog.Builder(getActivity())
				.setView(v)
				.setTitle(R.string.dialog_choice_title)
				.setPositiveButton("Date",new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						 choiceMade(0,Activity.RESULT_OK);
						
						
					}
				})
				.setNegativeButton("Time", new DialogInterface.OnClickListener() {// use external strings preferably 
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						choiceMade(1,Activity.RESULT_OK);
						
					}
				})
				.create();
	}

	public  static ChoiceFragment newInstance(String  choice) {
		Bundle args = new Bundle();
		args.putSerializable(EXTRA_CHOICE, choice);
		ChoiceFragment fragment = new ChoiceFragment();
		fragment.setArguments(args);
		return fragment;
	}
	
	
	void choiceMade(int choice, int resultCode) {
		if (getTargetFragment()==null) return;
		
		Intent i=new Intent();
		i.putExtra(EXTRA_CHOICE, mChoice);
		getTargetFragment()
		.onActivityResult(getTargetRequestCode(), resultCode, i);
		FragmentManager fm=getActivity().getSupportFragmentManager();
		
		switch(REQUEST_CHOICE){
		case 0:
			DatePickerFragment datedialog=DatePickerFragment.newInstance(mCrime.getDate()); //<-- Null Pointer Exception  here
			datedialog.setTargetFragment(this,REQUEST_CHOICE);
			datedialog.show(fm, DIALOG_DATE);
			break;
		case 1:
			TimePickerFragment timedialog=TimePickerFragment.newInstance(mCrime.getDate());
			timedialog.setTargetFragment(this,REQUEST_CHOICE);
			timedialog.show(fm, DIALOG_TIME);
			break;
		} 	
		

}
}	

TimePicker.java

package com.bignerdranch.criminalintent;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.TimePicker;

public class TimePickerFragment extends DialogFragment {
	public static final String EXTRA_TIME="com.bignerdranch.android.criminalintent.date";
	private Date mTime;
	
	private void sendResult (int resultCode){
		if (getTargetFragment()==null)
			return;
		
		Intent i=new Intent();
		i.putExtra(EXTRA_TIME, mTime);
		getTargetFragment()
		.onActivityResult(getTargetRequestCode(), resultCode, i);
	}
	public Dialog onCreateDialog(Bundle onSavedInstanceState){
		mTime=(Date)getArguments().getSerializable(EXTRA_TIME); //<--I get a Null Pointer Exception here
		final Calendar calendar=Calendar.getInstance();
		calendar.setTime(mTime);
		int hour=calendar.get(Calendar.HOUR_OF_DAY);
		int minute=calendar.get(Calendar.MINUTE);
		View v=getActivity().getLayoutInflater().inflate(R.layout.dialog_time, null);
		TimePicker timePicker=(TimePicker)v.findViewById(R.id.dialog_time_timePicker);
		timePicker.setIs24HourView(true);
		timePicker.setCurrentHour(hour);
		timePicker.setCurrentMinute(minute);
		timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
			
			@Override
			public void onTimeChanged(TimePicker view, int hour, int minute) {
				mTime=new GregorianCalendar(0,0,0,hour,minute).getTime();
				
			}
		});
		return new AlertDialog.Builder(getActivity())
		.setView(v)
		.setTitle(R.string.time_picker_title)
		.setPositiveButton(android.R.string.ok,
				new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						sendResult(Activity.RESULT_OK);
						
					}
				})
		.setView(v)
		.create();
	}
	public static TimePickerFragment newInstance(Date date){
		Bundle args=new Bundle();
		args.putSerializable(EXTRA_TIME, date);
		TimePickerFragment fragment=new TimePickerFragment();
		fragment.setArguments(args);
		return fragment;
	}

}

DatePicker.java

package com.bignerdranch.criminalintent;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;

public class DatePickerFragment extends DialogFragment {

	public static final String EXTRA_DATE = "com.bignerdranch.criminalintent.date";
	private Date mDate;

	private void sendResult(int resultCode) {
		if (getTargetFragment() == null)
			return;
		Intent i = new Intent();
		i.putExtra(EXTRA_DATE, mDate);
		getTargetFragment().onActivityResult(getTargetRequestCode(),
				resultCode, i);

	}

	public Dialog onCreateDialog(Bundle onSavedInstanceState) {
		mDate = (Date) getArguments().getSerializable(EXTRA_DATE);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(mDate);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);

		View v = getActivity().getLayoutInflater().inflate(
				R.layout.dialog_date, null);

		DatePicker datePicker = (DatePicker) v
				.findViewById(R.id.dialog_date_datePicker);
		datePicker.init(year, month, day, new OnDateChangedListener() {

			@Override
			public void onDateChanged(DatePicker view, int year, int month,
					int day) {
				mDate = new GregorianCalendar(year, month, day).getTime();
				getArguments().putSerializable(EXTRA_DATE, mDate);
			}

		});

		mDate = (Date) getArguments().getSerializable(EXTRA_DATE);
		return new AlertDialog.Builder(getActivity())
				.setView(v)
				.setTitle(R.string.date_picker_title)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								sendResult(Activity.RESULT_OK);

							}
						})
				    .create();
	}

	public static DatePickerFragment newInstance(Date date) {
		Bundle args = new Bundle();
		args.putSerializable(EXTRA_DATE, date);
		DatePickerFragment fragment = new DatePickerFragment();
		fragment.setArguments(args);
		return fragment;
	}
}

Would you help? thanks