6

I'm working on integrating a previously built Sitecore solution (web project as well as TDS items) into the current Sitecore solution we are building. I've been working through a variety of Application errors that have sprung up during this migration, but I seem to be stuck on this one related to Glassmapper. The application is throwing a NullReferenceException at the following line:

string.IsNullOrEmpty(Model.BackgroundImage) ? "bg-primary-dark" : string.Empty)" style="background-image: url('@Model.BackgroundImage.Src')

When I step through using the debugger, I can see that Model.BackgroundImage is null. However, the ID in the Model is set to the appropriate Sitecore item. So it seems as though Glassmapper is finding the item, but not resolving the fields correctly.

This is the Model itself. It was working without error before I began trying to integrate the old Sitecore solution:

using Glass.Mapper.Sc.Configuration.Attributes;

namespace Site.Models
{
    [SitecoreType(AutoMap=true, TemplateId= "{5DADD454-BB7D-46A4-AF64-0C169DBDAF79}")]
    public class HomePageHero : GlassBase, IHomePageHero
    {
        public new static string ModelTemplateId { get { return "{5DADD454-BB7D-46A4-AF64-0C169DBDAF79}"; } }

        public virtual string FirstTextLine { get; set; }

        public virtual string SecondTextLine { get; set; }

        public virtual string ThirdTextLine { get; set; }

        public virtual Glass.Mapper.Sc.Fields.Image BackgroundImage { get; set; }

        public virtual Glass.Mapper.Sc.Fields.Link ButtonLink { get; set; }
    }
}

And this is how I'm inheriting the Model in the .cshtml file:

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<HomePageHero>

Does anyone have an idea of what might be going on here?

  • 1
    Are any of the other fields null, or is it just the BackgroundImage field? Glass.Mapper tends to map complex fields in Sitecore to null values when they are left empty. In this case, can you double-check that the BackgroundImage field is populated in Sitecore? – Chris Smith Nov 8 '16 at 14:23
  • All of the fields are null. And the fields are all populated in Sitecore. – c-bro Nov 8 '16 at 14:25
  • 1
    do you have spaces in Sitecore for the fields? for example FirstTextLine is First Text Line – Tamás Tárnok Nov 8 '16 at 14:45
  • I do have spaces in the Sitecore fields. However, they were correctly resolving without spaces in the Model before I published the additional project. – c-bro Nov 8 '16 at 14:47
  • 1
    @c-bro that could be a problem, because then it does not find the related field. could you please try to remove the spaces or use [SitecoreField("First Text Line")] on the FirstTextLine property? i had the same issue some time ago. like here - glass.lu/Blog/AutoMapping – Tamás Tárnok Nov 8 '16 at 14:55
7

The problem could be that you use AutoMapping. If the field names are not exactly the same as the property names then Glass can't solve these. I would try to use [SitecoreField("First Text Line")] attribute for FirstTextLine property, if your field name in Sitecore is First Text Line. Here is the documentation about AutoMap - http://www.glass.lu/Blog/AutoMapping

| improve this answer | |
  • I've attempted to both change the Sitecore item field names to remove spaces, as well as add in the SitecoreField attributes, but neither seemed to do the trick. As the fields were originally AutoMapping correctly, I don't believe this is the source of the issue, but it definitely didn't hurt to double check it. – c-bro Nov 8 '16 at 15:10
  • So after looking at everything more closely AutoMapping was the issue. Something in the new project is breaking the AutoMapping that was previously working. I originally didn't catch that adding the SitecoreField attribute was working because I was still getting an error on Model.BackgroundImage, but this time it was the background image of the homepage Banner throwing the error, not the homepage Hero. Thank you for your help! – c-bro Nov 8 '16 at 15:49
  • Glad to help you :) – Tamás Tárnok Nov 8 '16 at 15:53
1

Does the item exist in the requested language?

What you're experiencing happens when you are trying to convert an Item to a Glass object, when that Item is does not have a version in the current context language.

Glass objects are usually instantiated in Glass.Mapper.Sc.SitecoreService:

public object CreateType(Type type, Item item, bool isLazy, bool inferType, Dictionary<string, object> parameters, params object[] constructorParameters)
{
    if (item == null || item.Versions.Count == 0 && Utilities.DoVersionCheck(this.Config))
        return (object) null;

    if (constructorParameters != null && constructorParameters.Length > 10)
        throw new NotSupportedException("Maximum number of constructor parameters is 10");

    SitecoreTypeCreationContext typeCreationContext = new SitecoreTypeCreationContext();

    // Creation context setup omitted ...
    return this.InstantiateObject((AbstractTypeCreationContext) typeCreationContext);
}

Sitecore able to resolve the Item even if it does not exist in the language (with a lot of null fields) - which is useful for shared fields - but when casting the item to a Glass object, GlassMapper checks whether a version of the item exists in the current language, and if it does not, the cast will return null.

| improve this answer | |
  • I originally thought this may be a language issue so I forced the language to English by appending ?sc_lang=en to the URL. I then double checked Sitecore to be sure the item exists in English which it does. When looking through the Glassmapper Model I also see the language of the item is set to "en" so that all appears to be as it should. – c-bro Nov 8 '16 at 14:58
  • @c-bro Can you fetch the item in the controller, and check the language and its contents? – Thorkil Holm-Jacobsen Nov 8 '16 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.