Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anthony Chudley 50 posts 197 karma points
    May 11, 2018 @ 13:55
    Anthony Chudley
    0

    Odd Null Reference Error

    Version: 7.10.4

    Hello fellow Umbracians, I need your help.

    Hoping I'm just doing something silly and I haven't had enough coffee to work out what's going wrong.

    In short, I have a navigation partial where I am looking up the sites home node, and then grabbing its children:

    Home home = Umbraco.TypedContentAtXPath("//home").Select(x => new Home(x)).FirstOrDefault();
    IEnumerable<IPublishedContent> pages = home.Children;
    

    That seems to be pretty good to me. And it works lovely on any child pages of the home page. However, it throws a System.NullReferenceException on the homepage.

    What makes this super weird is that as far as Visual Studio seems concerned the content is there:

    Example of null ref error

    Anyone got any ideas about what I might be missing?

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    May 11, 2018 @ 14:12
    Dan Diplo
    0

    I'm not sure why you'd get the null reference, but you can simplify your code to just be:

    var home = Model.Content.AncestorOrSelf<Home>();
    
  • Anthony Chudley 50 posts 197 karma points
    May 11, 2018 @ 14:20
    Anthony Chudley
    0

    Thanks Dan,

    I just fell into that pattern for retrieving area pages, so, for example, we have a settings node etc. outside of the site structure.

    Have tried with the Ancestor call (using ModelsBuilder so I dropped the '.Content') and still get the same error (also the same with '.Site').

Please Sign in or register to post replies

Write your reply to:

Draft