Copied to clipboard

Flag this post as spam?

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


  • gribchic 3 posts 23 karma points
    Jul 11, 2020 @ 15:10
    gribchic
    0

    Object reference not set to an instance of an object

    Hi I've added a new page home 2 Trying to open the page I've got the next issue:

    Controller Home2 and Action Home2 caused the following exception:

    Exception: Object reference not set to an instance of an object.

    at MySite.Controllers.Home2Controller.Home2(RenderModel renderModel)

    in d:\a\1\s\MySite\Controllers\Home2Controller.cs:line 21

    at lambda_method(Closure , ControllerBase , Object[] )

    at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)

    at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)

    at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)

    at System.Web.Mvc.Async.AsyncControllerActionInvoker.

    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)

    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()

    at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)

    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.

    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>cDisplayClass46.3f()

    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>cDisplayClass46.3f()

    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>cDisplayClass46.3f()

    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>cDisplayClass33.32(IAsyncResult asyncResult)

    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()

    at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)

    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>cDisplayClass21.<>cDisplayClass2b.

    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>cDisplayClass21.1e(IAsyncResult asyncResult)

    Home2 controller:

    using System.Web.Mvc;

    using Newtonsoft.Json;

    using MySite.Constants;

    using MySite.Contracts;

    using MySite.Extensions;

    using MySite.Helpers;

    using MySite.HttpClient;

    using MySite.Models;

    using Umbraco.Web;

    using Umbraco.Web.Models;

    using Umbraco.Web.Mvc;

    namespace MySite.Controllers { public class Home2Controller : RenderMvcController { // GET: Home

        public ActionResult Home2(RenderModel renderModel)
        {
    
            IRuntimeCacheWrapper runtimeCacheWrapper =
                new RuntimeCacheWrapper(ApplicationContext.ApplicationCache.RuntimeCache);
    
            UmbracoGridLayout _content = JsonConvert.DeserializeObject<UmbracoGridLayout>
                    (renderModel.Content.GetPropertyValue("content").ToString());
    
            Home2Model model = new Home2Model(renderModel.Content)
            {
    
                GridHeroBanner = JsonConvert.DeserializeObject<UmbracoGridLayout>
                    (renderModel.Content.GetPropertyValue("homeBanner").ToString()),
    
    
                Content = _content,
    
            };
    
            ReviewModel reviewModel = new ReviewModel(model.TestimonialType,
                new ReviewHelper(new ReviewsHttpClient(), runtimeCacheWrapper)
                , DefaultConstants.TestimonialHeading);
    
    
            model.ReviewModel = reviewModel;
    
            return View(model);
        }
    }
    

    }

    line 21: IRuntimeCacheWrapper runtimeCacheWrapper = new RuntimeCacheWrapper(ApplicationContext.ApplicationCache.RuntimeCache);

    Can anybody help me to fix the issue?

  • gribchic 3 posts 23 karma points
    Jul 13, 2020 @ 20:16
    gribchic
    0

    Resolved. The issue was in line:

    GridHeroBanner = JsonConvert.DeserializeObject

  • harlybake 3 posts 73 karma points
    Feb 01, 2021 @ 06:59
    harlybake
    0

    An Object is an instance of a Class , it is stored some where in memory. A reference is what is used to describe the pointer to the memory location where the Object resides. The message "object reference not set to an instance of an object" (NullReferenceException) means that you are referring to an object the does not exist or was deleted or cleaned up. In order to prevent the error, objects that could be null should be tested for null before being used.

    if (mClass != null)
    {
      // Go ahead and use mClass
      mClass.property = ...
    }
    else
    {
      // Attempting to use mClass here will result in NullReferenceException
    }
    

    A NullReferenceException typically reflects developer error and is thrown in the following scenarios:

    • Forgotten to instantiate a reference type.
    • Forgotten to dimension an array before initializing it.
    • Is thrown by a method that is passed null.
    • Get a null return value from a method, then call a method on the returned type.
    • Using an expression to retrieve a value and, although checking whether the value is null.
    • Enumerating the elements of an array that contains reference types, and attempt to process one of the elements.
Please Sign in or register to post replies

Write your reply to:

Draft