Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android Fatal Exception: java.lang.NullPointerException when opening the DatePickerDialog

I have an EditText when clicked it should open DatePickerDialog. But in some devices it is crashing randomly. In Crashlytics it is showing it is crashing in some devices with android version 8 and some in version 9. Please let me know where i am doing wrong.

For DatePicker i am setting minimum and maximum date a user can select.

DatePickerDialog dialog = new DatePickerDialog(mContext, date, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH));

            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            inputFormat.setTimeZone(TimeZone.getTimeZone("IST"));
            Date date = new Date();
            try {
                date = inputFormat.parse("2019-01-13T13:00:00Z"); // this is getting from server
            } catch (ParseException e) {
                e.printStackTrace();
            }

            Calendar minCal = Calendar.getInstance();
            minCal.setTime(date);
            dialog.getDatePicker().setMinDate(minCal.getTimeInMillis());
            dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
            // dialog.getDatePicker().setCalendarViewShown(false);
            dialog.show();

This is the crash message i am getting.

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.SimpleMonthView.getMonthHeight()' on a null object reference
       at android.widget.DayPickerView.onLayout(DayPickerView.java:230)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.widget.ScrollView.onLayout(ScrollView.java:1552)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
       at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at com.android.internal.widget.AlertDialogLayout.setChildFrame(AlertDialogLayout.java:356)
       at com.android.internal.widget.AlertDialogLayout.onLayout(AlertDialogLayout.java:349)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
       at com.android.internal.policy.DecorView.onLayout(DecorView.java:941)
       at android.view.View.layout(View.java:19962)
       at android.view.ViewGroup.layout(ViewGroup.java:6310)
       at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2671)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2376)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1488)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7182)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:935)
       at android.view.Choreographer.doCallbacks(Choreographer.java:747)
       at android.view.Choreographer.doFrame(Choreographer.java:677)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:921)
       at android.os.Handler.handleCallback(Handler.java:790)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:192)
       at android.app.ActivityThread.main(ActivityThread.java:6806)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:445)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)
like image 367
Rajeev Shetty Avatar asked Apr 30 '19 06:04

Rajeev Shetty


People also ask

How do I fix null pointer exception in Android?

How to fix the NullPointerException? To avoid NullPointerException we have to initialize the Textview component with the help of findviewbyid( ) method as shown below. The findViewbyId( ) takes the “id” value of the component as the parameter. This method helps locate the component present in the app.

How do I fix Java Lang NullPointerException?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

What does error Java Lang NullPointerException mean?

The java. lang. NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.


1 Answers

Issue was the minDate was higher than the maxDate in some random cases because of some server issues.I was not handling the scenario thinking the minDate(What i was getting) will always be lesser than the System.currentTimeMillis(). So need to handle the scenario :

            if(System.currentTimeMillis() > minCal.getTimeInMillis()){
                dialog.getDatePicker().setMinDate(minCal.getTimeInMillis());
                dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
                dialog.getDatePicker().setCalendarViewShown(false);
                dialog.show();
            }else{

                //Error message depending on the requirements.

            }

like image 112
Rajeev Shetty Avatar answered Oct 16 '22 19:10

Rajeev Shetty