Sankalp_1 0 Newbie Poster

I have done a lot of research on this issue that I am getting, but I am unable to solve this error. Over the Internet, folks are suggesting to use findFragmentBy ID or Tag, but both of them resulting in null Pointer Exception. This is the Code that I have written:

This is the main fragment that is hosting this update button. As intended, this button opens a dialog to select either date or time and then fires up another dialog that allows the user to edit date or time. I am setting the setTargetFragment here.

updateInfo= (Button) v.findViewById(R.id.update_info);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentData = sdf.format(crime.getDate());
        SimpleDateFormat sdfT = new SimpleDateFormat("HH:mm");
        String currentDataTime = sdfT.format(crime.getDate());
        updateInfo.setText(currentData+" "+currentDataTime);
        updateInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm=getActivity().getSupportFragmentManager();
                Date date=crime.getDate();
                UpdateSelector updateSelector=UpdateSelector.newInstance(date);
                updateSelector.setTargetFragment(CrimeFragment.this,getRequest_code_updateSelector);
                updateSelector.show(fm,Update_Selector);
            }
        });

Following is the code for the updateSelector class

public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        date=(Date) getArguments().getSerializable(tag);
        builder.setTitle("Select");
        builder.setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0)
                    selected = which;
                if (which == 1)
                    selected = which;
        }
    });
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (selected == 0) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                DatePickerFragment datePicker = DatePickerFragment.newInstance(date);
                datePicker.show(fm, datepickertag);
            }
            if (selected == 1) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                TimePickerFragment timePicker = TimePickerFragment.newInstance(date);
                timePicker.show(fm, timepickertag);

            }
        }
    });
    return builder.create();
  }

    public void sendResult(int resultCode)
{

    if(getTargetFragment== null)
       return;
    Intent intent=new Intent();
    date= (Date) getArguments().getSerializable(tag);
    intent.putExtra(tag, date);
    getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent);

}

public static UpdateSelector newInstance(Date date)
{

    Bundle args=new Bundle();
    args.putSerializable(tag, date);
    UpdateSelector updateSelector=new UpdateSelector();
    updateSelector.setArguments(args);
    return updateSelector;
}

I have designed datePickerFragment and TimePickerFragment that opens up the second dialog(Spinner to edit). Therefore, I am using them to send result to the CrimeFragment.

public class DatePickerFragment extends DialogFragment {
    public final static String Extra_Date="date";
    private Date date;
    UpdateSelector update;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
       // super.onCreate(savedInstanceState);
        date=(Date) getArguments().getSerializable(Extra_Date);
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH);
        int day=calendar.get(Calendar.DAY_OF_MONTH);
    View view=getActivity().getLayoutInflater().inflate(R.layout.date_picker, null);
     DatePicker datePicker= (DatePicker) view.findViewById(R.id.date_button);
    datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            date=new GregorianCalendar(year,monthOfYear,dayOfMonth).getTime();
            update=UpdateSelector.newInstance(date);
            getArguments().putSerializable(Extra_Date, date);
        }
    });
    return new AlertDialog.Builder(getActivity()).setView(view).setTitle(R.string.date_picker).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            update.sendResult(Activity.RESULT_OK);
        }
    }).create();
}

Now, the update.sendResult(Activity.OK) sends result to this method

 public void sendResult(int resultCode)
        {

        if(getTargetFragment== null)
           return;
        Intent intent=new Intent();
        date= (Date) getArguments().getSerializable(tag);
        intent.putExtra(tag, date);
        getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent);

    }

Where fragment is null. Please Explain in detail. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.