简体   繁体   中英

Why am I getting a a null object reference error?

I am trying to show a custom minute interval in the time picker, so here is what I'm trying to do. In my MainActivity I have this method(called on a button click),which is opening ad DialogFragment :

public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }

Here is the TimePickerFragment :

public class TimePickerFragment extends DialogFragment
        implements DurationTimePickDialog.OnTimeSetListener {


    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = 0;

        // Create a new instance of CustomTimePickerDialog and return it
        return new CustomTimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        String realMinute= "";

        switch (minute) {
            case 1:
                realMinute = "01";
                break;
            case 2:
                realMinute = "02";
                break;
            case 3:
                realMinute = "03";
                break;
            case 4:
                realMinute = "04";
                break;
            case 5:
                realMinute = "05";
                break;
            case 6:
                realMinute = "06";
                break;
            case 7:
                realMinute = "07";
                break;
            case 8:
                realMinute = "08";
                break;
            case 9:
                realMinute = "09";
                break;
            default:
                realMinute = String.valueOf(minute);
                break;
        }

        final String selectedDate = String.valueOf(hourOfDay) + ":" + realMinute;
        EditText selectedTimeTxt = (EditText) getActivity().findViewById(R.id.selectedTime);
        selectedTimeTxt.setText(selectedDate);
    }
}

As you can see I'm using a customized class called CustomTimePickerDialog in order to show the dialog with the 10 minute interval. Here the code is:

public class CustomTimePickerDialog extends TimePickerDialog {

    private final static int TIME_PICKER_INTERVAL = 5;
    private TimePicker timePicker;
    private final OnTimeSetListener callback;

    public CustomTimePickerDialog(Context context, OnTimeSetListener callBack,
                                  int hourOfDay, int minute, boolean is24HourView) {
        super(context, callBack, hourOfDay, minute / TIME_PICKER_INTERVAL,
                is24HourView);
        this.callback = callBack;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (callback != null && timePicker != null) {
            timePicker.clearFocus();
            callback.onTimeSet(timePicker, timePicker.getCurrentHour(),
                    timePicker.getCurrentMinute() * TIME_PICKER_INTERVAL);
        }
    }

    @Override
    protected void onStop() {
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        try {
            Class<?> classForid = Class.forName("com.android.internal.R$id");
            Field timePickerField = classForid.getField("timePicker");
            this.timePicker = (TimePicker) findViewById(timePickerField
                    .getInt(null));
            Field field = classForid.getField("minute");

            NumberPicker mMinuteSpinner = (NumberPicker) timePicker
                    .findViewById(field.getInt(null));
            mMinuteSpinner.setMinValue(0);
            mMinuteSpinner.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
            List<String> displayedValues = new ArrayList<String>();
            for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
                displayedValues.add(String.format("%02d", i));
            }
            mMinuteSpinner.setDisplayedValues(displayedValues
                    .toArray(new String[0]));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

The problem is that I'm getting this strange for me error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.NumberPicker.setMinValue(int)' on a null object reference regarding this line: mMinuteSpinner.setMinValue(0); - in the CustomTimePickerDialog class.

I know that I'm messing the things here(probably in the TimePickerFragment class), but as an android and java beginner I'm not able to spot my mistake.

Can you give me a clue?

NumberPicker mMinuteSpinner = (NumberPicker) timePicker
                .findViewById(field.getInt(null));

is presumably returning null .

android.widget.NumberPicker.setMinValue(int) on a null object reference so it says its being called on null object that means mMinuteSpinner is null for sure the JVM doesn't go wrong dude just debug your code If you need help i'll help you debug

NumberPicker mMinuteSpinner = (NumberPicker) timePicker .findViewById(field.getInt(null));

        mMinuteSpinner.setMinValue(0);
LayoutInflater l = getLayoutInflater();
final View v = l.inflate(R.layout.create_alarm_dialog, null);

//get the number pickers
NumberPicker hoursNP = (NumberPicker) v.findViewById(R.id.hours);

This is how I did mine, I'm guessing you need to specify the view so it knows where to look for the ID. Sorry I'm also a noob at android and this is my first stackover flow post. I hope this helps.

  mMinuteSpinner.setMinValue(0);

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.NumberPicker.setMinValue(int)' on a null object reference

Blockquote

Still crash in android version 11 API level 30 like Oppo, Samsung, Realme, OnePlus, etc. mobile devices

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM