In the release notes for version 10.3 we found the matching bug:
If a Responsive Image variant field points to a broken link, a
NullReferenceException error occurs.
Upgrading is 10.3 is not an option though. But I found a way to "patch" the issue. The problem is located in RenderFallbackResponsiveMImage which uses the (deprecated) StylelabsDataExtractor.GetSrcAttributeValue. Replacing that with the new version for that function actually already solves the issue.
Note that this code came with the hotfix (10.2.1).
If you want to fix this without upgrading, create a class like this:
using System.Web.UI;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Web.UI.WebControls;
using Sitecore.XA.Feature.Stylelabs.Services;
using Sitecore.XA.Foundation.Variants.Abstractions.Pipelines.RenderVariantField;
namespace Foundation.Infrastructure
{
public class RenderFallbackResponsiveMImage : Sitecore.XA.Feature.Stylelabs.Pipelines.RenderVariantField.RenderFallbackResponsiveMImage
{
public RenderFallbackResponsiveMImage(IStylelabsDataExtractor stylelabsDataExtractor) : base(stylelabsDataExtractor)
{
}
public override void RenderField(RenderVariantFieldArgs args)
{
var variantField = args.VariantField;
if (args.Item.Paths.IsMediaItem || string.IsNullOrWhiteSpace(variantField?.FieldName))
{
base.RenderField(args);
}
else if (PageMode.IsExperienceEditorEditing)
{
args.ResultControl = (Control)new FieldRenderer()
{
Item = args.Item,
FieldName = variantField.FieldName,
DisableWebEditing = !args.IsControlEditable
};
args.Result = this.RenderControl(args.ResultControl);
}
else
{
var field1 = args.Item.Fields[variantField.FieldName];
if (field1 == null)
{
return;
}
if ((FieldTypeManager.GetField(field1) is ImageField field2 ? field2.MediaItem : (Item)null) == null)
{
field2 = (ImageField)field1;
string srcAttributeValue = this.StylelabsDataExtractor.GetAttributeValue("src", field2);
Control fieldRenderer = this.CreateFieldRenderer(args);
this.RenderControl(args, fieldRenderer, srcAttributeValue);
}
else
{
base.RenderField(args);
}
}
}
}
}
Note that we are using this.StylelabsDataExtractor.GetAttributeValue("src", field2) now - this is the line that solves the problem.
Patch the config:
<sitecore>
<pipelines>
<renderVariantField>
<processor patch:instead="*[@type='Sitecore.XA.Feature.Stylelabs.Pipelines.RenderVariantField.RenderFallbackResponsiveMImage, Sitecore.XA.Feature.Stylelabs']"
type="Foundation.Infrastructure.RenderFallbackResponsiveMImage, Foundation.Infrastructure" resolve="true" />
</renderVariantField>
</pipelines>
</sitecore>
and you're good to go.
ps: Don't forget to delete this code if you upgrade to 10.3.