Copied to clipboard

Flag this post as spam?

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


  • ggesheva 15 posts 116 karma points
    May 11, 2023 @ 05:36
    ggesheva
    0

    Model Builder: Non-Required Fields Expecting Input and Causing Null Reference Exception

    Hi everyone,

    I've recently been working on a project using Umbraco v10.4.2, and I'm having some issues with the model builder. Specifically, I've noticed that some of the fields generated by the model builder are expecting an input even when the field is not marked as required in the back office. This issue is causing the generated .cs model to throw a "System.NullReferenceException: Object reference not set to an instance of an object." error.

    enter image description here

    I found that this error is occurring because the _publishedValueFallback property is staying null for the fields that are not marked as required. This is causing issues when the model is generated and the code tries to access these fields.

    I'm wondering if anyone else has experienced this issue and if there are any workarounds or fixes that can be implemented. I've tried looking through the Umbraco documentation and forum, but I haven't been able to find a solution yet.

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    May 22, 2023 @ 14:27
    Frans de Jong
    0

    Most of the times weird issues like this are caused by the code calling the property. Can you paste a snipped of the code that refers to this model?

  • Jimmy 5 1 post 71 karma points
    Nov 10, 2023 @ 17:56
    Jimmy 5
    0

    I figured it out!!! The issue is that your content model is missing a fallback.

    Here is a simplified example from within a Home Page RenderController using Umbraco 12:

    using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    
    public class HomeController : RenderController
     {
         private IPublishedValueFallback _publishedValueFallback;
         private readonly ServiceContext _serviceContext;
         private readonly IVariationContextAccessor _variationContextAccessor;
    
    
         public HomeController(
                 ServiceContext context, 
                 ICompositeViewEngine compositeViewEngine,
                 IUmbracoContextAccessor umbracoContextAccessor,
                 IPublishedValueFallback publishedValueFallback, 
                 IVariationContextAccessor variationContextAccessor
              )
             : base(compositeViewEngine, umbracoContextAccessor)
             {
                 _publishedValueFallback = publishedValueFallback;
                 _serviceContext = context;
                 _variationContextAccessor = variationContextAccessor;
             }
    
         public override IActionResult Index()
         {
             //This is the normal method of including a fallback.
             //In this case, we are using the injected fallback since this model is related to the current page coming in.
             var cmHome = new ContentModels.Home(CurrentPage, _publishedValueFallback);
    
    
             //Loop through the child nodes:
             foreach (IPublishedContent ipChild in cmHome.Children)
             {
                //Here we want to instantiate a model different from the parent. 
                //In this case we want to create a new fallback.
                ContentModels.ChildPg cmChild = new ContentModels.ChildPg(ipChild, new PublishedValueFallback(_serviceContext, _variationContextAccessor));
    
                //Now you can check for null without having a NullReferenceException being thrown.
                if (cmChild.myProperty != null)
                {
                    //Get the values
                }
             }
        ...
    
Please Sign in or register to post replies

Write your reply to:

Draft