material-components/material-components-android · error · IllegalStateException

dateSelector should not be null. Use MaterialTextInputPicker

Error message

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.

What it means

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.

Source

Thrown at lib/java/com/google/android/material/datepicker/MaterialTextInputPicker.java:110

            for (OnSelectionChangedListener<S> listener : onSelectionChangedListeners) {
              listener.onSelectionChanged(selection);
            }
          }

          @Override
          public void onIncompleteSelectionChanged() {
            for (OnSelectionChangedListener<S> listener : onSelectionChangedListeners) {
              listener.onIncompleteSelectionChanged();
            }
          }
        });
  }

  @NonNull
  @Override
  public DateSelector<S> getDateSelector() {
    if (dateSelector == null) {
      throw new IllegalStateException(
          "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.");
    }
    return dateSelector;
  }
}

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Always create the fragment with MaterialTextInputPicker.newInstance(dateSelector).
  2. Only call getDateSelector() after the fragment has been created (onCreate or later, e.g. inside onActivityCreated/onViewCreated or from the parent picker).
  3. In tests, use FragmentScenario with the factory-produced instance rather than direct construction.

Example fix

// before
val frag = MaterialTextInputPicker()
frag.getDateSelector() // throws: arguments never set

// after
val frag = MaterialTextInputPicker.newInstance(dateSelector)
// ... after fragment creation (e.g. in onCreate/onViewCreated):
val selector = frag.getDateSelector()
Defensive patterns

Strategy: validation

Validate before calling

// Always use the factory; only read the selector after the fragment is created
val picker = MaterialTextInputPicker.newInstance(dateSelector)
picker.show(childFragmentManager, tag)
// later, e.g. in a callback after onCreate:
if (picker.isAdded) {
  val selector = picker.dateSelector
}

Type guard

fun MaterialTextInputPicker<S>.hasDateSelector(): Boolean = try { dateSelector; true } catch (e: IllegalStateException) { false }

Try / catch

catch IllegalStateException around getDateSelector and surface 'fragment not created via newInstance()' as a developer configuration error (assert in debug builds).

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14). Data as JSON: /api/errors/956636b708dccaa5. Report an issue: GitHub.