{"record":{"id":"24b26a1db6216a61","repo":"material-components/material-components-android","slug":"sheet-view-reference-is-null-sheet-edge-cannot-be","errorCode":null,"errorMessage":"Sheet view reference is null; sheet edge cannot be changed if the sheet view is null.","messagePattern":"Sheet view reference is null; sheet edge cannot be changed if the sheet view is null\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/sidesheet/SheetDialog.java","lineNumber":343,"sourceCode":"            }\n            return super.performAccessibilityAction(host, action, args);\n          }\n        });\n    return container;\n  }\n\n  /**\n   * Set the edge which the sheet should originate from.\n   *\n   * <p>Note: This method should be called when the sheet is initialized, before it is shown.\n   * Runtime sheet edge changes are not supported.\n   *\n   * @throws IllegalStateException if the sheet is null or has already been laid out\n   * @param gravity the edge from which the sheet and its animations should originate.\n   */\n  public void setSheetEdge(@GravityInt int gravity) {\n    if (sheet == null) {\n      throw new IllegalStateException(\n          \"Sheet view reference is null; sheet edge cannot be changed if the sheet view is null.\");\n    }\n    if (sheet.isLaidOut()) {\n      throw new IllegalStateException(\n          \"Sheet view has been laid out; sheet edge cannot be changed once the sheet has been laid\"\n              + \" out.\");\n    }\n    ViewGroup.LayoutParams layoutParams = sheet.getLayoutParams();\n    if (layoutParams instanceof CoordinatorLayout.LayoutParams) {\n      ((CoordinatorLayout.LayoutParams) layoutParams).gravity = gravity;\n      maybeUpdateWindowAnimationsBasedOnLayoutDirection();\n    }\n  }\n\n  private void maybeUpdateWindowAnimationsBasedOnLayoutDirection() {\n    Window window = getWindow();\n    if (window != null\n        && sheet != null","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/sidesheet/SheetDialog.java#L325-L361","documentation":"SheetDialog.setSheetEdge() throws IllegalStateException when the internal sheet view reference is null — i.e. the dialog's content view (and thus the sheet) has not been created yet. The method needs a real sheet view to adjust its CoordinatorLayout.LayoutParams gravity; the Javadoc states it must be called when the sheet is initialized, before it is shown.","triggerScenarios":"Calling sideSheetDialog.setSheetEdge(Gravity.LEFT) immediately after constructing the SheetDialog, before show()/setContentView() have inflated the sheet layout into the dialog window.","commonSituations":"Configuring a freshly constructed SideSheetDialog in a builder function that runs before show(); extracting the sheet edge to a helper called from onCreate() before super.onCreate() inflates content; refactoring dialog setup code so the edge call happens too early.","solutions":["Call setSheetEdge() after the sheet exists — e.g. inside your dialog's onCreate() after setContentView(...), or immediately after show() but before the sheet has been laid out.","Or set the edge declaratively so no runtime call is needed (the default right-edge behavior), and only override gravity before layout.","Guard with a null check: if (dialog.getSheet() != null) { dialog.setSheetEdge(gravity); } else { defer via dialog.setOnShowListener(...); }"],"exampleFix":"// before\nval dialog = SideSheetDialog(context)\ndialog.setSheetEdge(Gravity.LEFT) // sheet == null, throws\n\ndialog.show()\n\n// after\nval dialog = SideSheetDialog(context)\ndialog.show()\ndialog.setSheetEdge(Gravity.LEFT) // sheet exists, not yet laid out","handlingStrategy":"validation","validationCode":"if (dialog.findViewById(com.google.android.material.R.id.m3_side_sheet) != null) {\n  dialog.setSheetEdge(gravity);\n} else {\n  dialog.setOnShowListener(d -> dialog.setSheetEdge(gravity));\n  dialog.show();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat setSheetEdge as a create-time call: configure it right after show() or in onCreate() after setContentView().","Never call sheet APIs on a dialog that has not been shown."],"tags":["android","material-components","sidesheet","dialog","lifecycle"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}