{"record":{"id":"956636b708dccaa5","repo":"material-components/material-components-android","slug":"dateselector-should-not-be-null-use-materialtexti","errorCode":null,"errorMessage":"dateSelector should not be null. Use MaterialTextInputPicker#newInstance() to create this fragment with a DateSelector, and call this method after the fragment has been created.","messagePattern":"dateSelector should not be null\\. Use MaterialTextInputPicker#newInstance\\(\\) to create this fragment with a DateSelector, and call this method after the fragment has been created\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/datepicker/MaterialTextInputPicker.java","lineNumber":110,"sourceCode":"            for (OnSelectionChangedListener<S> listener : onSelectionChangedListeners) {\n              listener.onSelectionChanged(selection);\n            }\n          }\n\n          @Override\n          public void onIncompleteSelectionChanged() {\n            for (OnSelectionChangedListener<S> listener : onSelectionChangedListeners) {\n              listener.onIncompleteSelectionChanged();\n            }\n          }\n        });\n  }\n\n  @NonNull\n  @Override\n  public DateSelector<S> getDateSelector() {\n    if (dateSelector == null) {\n      throw new IllegalStateException(\n          \"dateSelector should not be null. Use MaterialTextInputPicker#newInstance() to create\"\n              + \" this fragment with a DateSelector, and call this method after the fragment has\"\n              + \" been created.\");\n    }\n    return dateSelector;\n  }\n}\n","sourceCodeStart":92,"sourceCodeEnd":118,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/datepicker/MaterialTextInputPicker.java#L92-L118","documentation":"MaterialTextInputPicker is meant to be instantiated via newInstance(dateSelector), which stores the selector in the fragment's arguments. getDateSelector() throws IllegalStateException when that field is still null — i.e. the fragment was constructed directly (default constructor) or the method is called before the arguments were read. The message explicitly directs you to the supported creation path.","triggerScenarios":"new MaterialTextInputPicker() followed by getDateSelector(); calling getDateSelector() before the fragment's onCreate has run (e.g. immediately after constructing/adding but before the fragment lifecycle processes arguments); fragment recreation where the default constructor is used and arguments were never set.","commonSituations":"Bypassing the factory because Data Binding or a DI framework constructs fragments reflectively; automated tests instantiating the fragment directly; accessing the selector in onAttach/create before the stored field is populated.","solutions":["Always create the fragment with MaterialTextInputPicker.newInstance(dateSelector).","Only call getDateSelector() after the fragment has been created (onCreate or later, e.g. inside onActivityCreated/onViewCreated or from the parent picker).","In tests, use FragmentScenario with the factory-produced instance rather than direct construction."],"exampleFix":"// before\nval frag = MaterialTextInputPicker()\nfrag.getDateSelector() // throws: arguments never set\n\n// after\nval frag = MaterialTextInputPicker.newInstance(dateSelector)\n// ... after fragment creation (e.g. in onCreate/onViewCreated):\nval selector = frag.getDateSelector()","handlingStrategy":"validation","validationCode":"// Always use the factory; only read the selector after the fragment is created\nval picker = MaterialTextInputPicker.newInstance(dateSelector)\npicker.show(childFragmentManager, tag)\n// later, e.g. in a callback after onCreate:\nif (picker.isAdded) {\n  val selector = picker.dateSelector\n}","typeGuard":"fun MaterialTextInputPicker<S>.hasDateSelector(): Boolean = try { dateSelector; true } catch (e: IllegalStateException) { false }","tryCatchPattern":"catch IllegalStateException around getDateSelector and surface 'fragment not created via newInstance()' as a developer configuration error (assert in debug builds).","preventionTips":["Never instantiate MaterialTextInputPicker with the default constructor.","Access the selector only after onCreate (isAdded == true).","In tests use FragmentScenario.launch(factory) style creation."],"tags":["android","material-components","datepicker","fragment","lifecycle"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}