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);
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.
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.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase
1.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
}
line 21: IRuntimeCacheWrapper runtimeCacheWrapper = new RuntimeCacheWrapper(ApplicationContext.ApplicationCache.RuntimeCache);
Can anybody help me to fix the issue?
Resolved. The issue was in line:
GridHeroBanner = JsonConvert.DeserializeObject
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.
A NullReferenceException typically reflects developer error and is thrown in the following scenarios:
is working on a reply...