Copied to clipboard

Flag this post as spam?

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


  • Sean Dooley 288 posts 527 karma points
    May 19, 2020 @ 10:41
    Sean Dooley
    1

    Nested Content | Object reference not set to an instance of an object.

    I've created the following Nested Content Data Type

    • Document Types Selected 1 Document Type
    • Min Items [blank]
    • Max Items [blank]

    When using it on a page, the editor could leave it blank, add one or multiple items.

    Using the following line, I always get Object reference not set to an instance of an object.

    var items = node.Value<IEnumerable<IPublishedElement>>("items");
    

    Any ideas?


    Umbraco 8.5.4

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    May 19, 2020 @ 10:53
    Alex Skrypnyk
    0

    Hi Sean,

    can you add a check that the node has a value in "items" property, like this:

    if(node.HasValue("items"))
    {
    var items = node.Value<IEnumerable<IPublishedElement>>("items");
    }
    

    Another case is that you need to handle nested content differently when only one item selected :

    if(node.HasValue("items"))
    {
    var onlyOneItemSelected= node.Value<IPublishedElement>("items");
    }
    

    Thanks,

    Alex

  • Sean Dooley 288 posts 527 karma points
    May 19, 2020 @ 14:08
    Sean Dooley
    0

    Thanks @Alex, adding .HasValue hasn't made any difference.

    In this scenario, the property could have 0, 1 or more items selected. With that in mind, I tried the following but was still getting the same error.

    var list = new List<IPublishedElement>();
    if(node.HasValue("items))
    {
      // Try get to multiple items
      try
      {
        var items = node.Value<IEnumerable<IPublishedElement>>("items");
        if(items != null)
        {
          list.AddRange(items);
        }
      }
      catch(Exception exception)
      {
        // If error is thrown getting multiple items, try getting single item
        var item = node.Value<IPublishedElement>("items");
        if(item != null)
        {
          list.Add(item);
        }
      }
    }
    

    I've managed to make some progress with the following tests

    Nested Content | Single Document Type | Single item mode

    Populated with one item, node.Value<IPublishedElement> works as expected.

    Nested Content | Multiple Document Types | Multiple Item mode

    Populated with one or more items, node.Value<IEnumerable<IPublishedElement>> works as expected.

    Nested Content | Single Document Type | Multiple Item mode

    Popuated with one or more items, both node.Value<IPublishedElement> and node.Value<IEnumerable<IPublishedElement>> return Object reference not set to an instance of an object.

  • Guido Adam 21 posts 65 karma points
    Jan 26, 2021 @ 19:57
    Guido Adam
    1

    When not using modelbuilder this can be a pain. It seems that adding a second (dummy) element to your nested content config, fixes this.

Please Sign in or register to post replies

Write your reply to:

Draft