NullReferenceException is thrown when loading SXA websites


Description

The SXA site pages might fail to load with an unhandled exception. The following message can be found in the logs:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Sitecore.XA.Feature.SiteMetadata.Services.ExternalLinkGenerator.GetExternalUrl(Item item)
   at Sitecore.XA.Feature.SiteMetadata.Repositories.OpenGraphMetadata.OpenGraphMetadataRepository.GetMetaTags()
   at Sitecore.XA.Feature.SiteMetadata.Repositories.MetadataBaseRepository`1.GetModel()
   at Sitecore.XA.Feature.SiteMetadata.Controllers.OpenGraphMetadataController.GetModel()
   at Sitecore.XA.Feature.SiteMetadata.Controllers.MetadataController.Index()
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass24_0.<InvokeActionMethodWithFilters>b__0()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

The issue occurs intermittently.

Solution

To resolve the issue, consider one of the following options: