"Object reference not set to an instance of an object" error occurs when attempting to preview an item


Description

Previewing an item in Pages, the Experience Editor, or the Content Editor might not be possible and might result in the error below when the item is viewed by the default\anonymous user.

Exception: System.NullReferenceException
Message: Object reference not set to an instance of an object.
Source: Sitecore.XA.Foundation.Theming
at Sitecore.XA.Foundation.Theming.ThemingContext.GetThemeItemInternal(Item item, DeviceItem device)
at Sitecore.XA.Foundation.SitecoreExtensions.Extensions.ContextExtensions.GetScopedValue[T](IContext context, String key, Func`1 func)
at Sitecore.XA.Foundation.Theming.ThemingContext.GetThemeItem(Item item, DeviceItem device)
at Sitecore.XA.Foundation.Theming.CompatibilityMode.CompatibilityModeProvider.GetCompatibilityMode(Item currentItem, DeviceItem device)
at Sitecore.XA.Foundation.Theming.Pipelines.HttpRequestBegin.AppendCompatibilityModeHeader.Process(HttpRequestArgs args)

Solution

To prevent the issue occurring, grant read access to the anonymous user, ensuring that the default/anonymous user can preview items.

Note: Make sure that all the items under /sitecore/system/Settings/Foundation/Experience Accelerator/Theming/Theming Modes and their parent items have read access granted for the anonymous user.