florent37 / SingleDateAndTimePicker

You can now select a date and a time with only one widget !
Apache License 2.0
1.02k stars 334 forks source link

NPE (java.util.TimeZone.getOffset(long)) when selecting dates #271

Open ardevd opened 4 years ago

ardevd commented 4 years ago

Given the following code on v2.2.3:

new SingleDateAndTimePickerDialog.Builder(getContext())
                    //.bottomSheet()
                    .curved()
                    .mustBeOnFuture()
                    .mainColor(ContextCompat.getColor(getContext(), R.color.primary))
                    //.minutesStep(15)
                    .displayListener(picker -> {
                        //retrieve the SingleDateAndTimePicker
                    })

                    .title(getString(R.string.departures_add_set_departure_time))
                    .listener(date -> {
                        timerDate = date;
                        updateTimeIndicator();
                        callback.onTimeSet(0);

                    }).display();

I get a crash whenever I touch the picker.

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.TimeZone.getOffset(long)' on a null object reference
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2356)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2321)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at java.util.Calendar.setTimeInMillis(Calendar.java:1787)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at java.util.Calendar.setTime(Calendar.java:1749)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at com.github.florent37.singledateandtimepicker.SingleDateAndTimePicker.getDate(SingleDateAndTimePicker.java:469)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at com.github.florent37.singledateandtimepicker.SingleDateAndTimePicker.updateListener(SingleDateAndTimePicker.java:534)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at com.github.florent37.singledateandtimepicker.SingleDateAndTimePicker.access$000(SingleDateAndTimePicker.java:38)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at com.github.florent37.singledateandtimepicker.SingleDateAndTimePicker$5.onDaySelected(SingleDateAndTimePicker.java:193)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at com.github.florent37.singledateandtimepicker.widget.WheelDayPicker.onItemSelected(WheelDayPicker.java:63)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at com.github.florent37.singledateandtimepicker.widget.WheelDayPicker.onItemSelected(WheelDayPicker.java:19)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at com.github.florent37.singledateandtimepicker.widget.WheelPicker.onItemSelected(WheelPicker.java:658)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at com.github.florent37.singledateandtimepicker.widget.WheelPicker.access$600(WheelPicker.java:39)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at com.github.florent37.singledateandtimepicker.widget.WheelPicker$1.run(WheelPicker.java:116)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:790)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:164)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:6494)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
04-23 21:35:14.412 32071 32071 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
MrAnjosi commented 4 years ago

Hello is easy correct this error, in your call add this function

.setTimeZone(TimeZone.getDefault())

This error occurred when to usage the timeZone

momadthenomad commented 4 years ago

While the suggestion fixes the issue, this issue should've not happened in the first place. The timezone should be defaulted so that unless you actually need to change the timezone, it should just work out of the box.

florent37 commented 4 years ago

I fixed this in the 2.2.4, can you try ?

https://github.com/florent37/SingleDateAndTimePicker/commit/09c61b42d688bf79537d30476d966f3faf1b5eb4