Object reference not set to an instance of an object.

Apr 17, 2012 at 7:59 PM

Can anyone please provide guidance on why I am receiving this error? any help would be greatly appreciated!

@(Html.Telerik().Grid<GetSalesTotalsForAllRegions_Result>()

               .Name("Grid")
               .BindTo((IEnumerable) ViewBag.Data)
               .Columns(columns => { columns.Bound(d => d.regionName).Width(80); })

               .DataBinding(dataBinding => dataBinding.Server()
                                               .Select<DashboardController>(ctr => ctr.CustomServerBinding(null)))
               .EnableCustomBinding(true)
               .Sortable()
      )

 

the controller never gets called, so I don't think that is the issue. Thank you!

Apr 17, 2012 at 8:05 PM

so, what's the error message?

Apr 17, 2012 at 8:12 PM

Object reference not set to an instance of an object.

as soon as the grid tries to load

Apr 17, 2012 at 8:46 PM

oh, right.  Didn't see the error in the title.  Once of your variables is null.  Possibly ViewBag.Data.  You can set a breakpoint in do some debugging to see which variable it is.  The exception should tell you what the variable is as well.  I would also recommend against using a ViewBag and to pass in a strongly typed model.  Here's Telerik's example http://demos.telerik.com/aspnet-mvc/razor/grid/customserverbinding

Coordinator
Apr 17, 2012 at 9:48 PM

In addition to what Noyabronok has suggested, please inform the structures of your Models and ViewModels as well as the mappings between them.

Also the full stacktrace could be of great value to identify the problem.

Apr 18, 2012 at 5:03 PM

thank you very much for your help so far.

  • The full stack trace is provided at the end
  • CustomServerBinding is never getting called (breakpoint not hit),. I'm pretty certain this is where the problem lies
  • I have used the Telerik model and try to pass back a strongly typed model, same results
  • my model is a complex type returned from a stored procedure
  • I put the most simple project possible together and uploaded it to http://quadsoftpa.com/_client/TelerikMvcApplication2.zip. it shows the error

thanks again.

[NullReferenceException: Object reference not set to an instance of an object.]   Telerik.Web.Mvc.Extensions.EnumerableExtensions.AsGenericEnumerable(IEnumerable source) +117   Telerik.Web.Mvc.UI.GridDataProcessor.GetCustomDataSource(IEnumerable dataSource) +73   Telerik.Web.Mvc.UI.GridDataProcessor.EnsureDataSourceIsProcessed() +747   Telerik.Web.Mvc.UI.GridDataProcessor.get_ProcessedDataSource() +29   Telerik.Web.Mvc.UI.Grid`1.CreateRenderingData() +386   Telerik.Web.Mvc.UI.Grid`1.WriteHtml(HtmlTextWriter writer) +1241   Telerik.Web.Mvc.UI.ViewComponentBase.ToHtmlString() +121   Telerik.Web.Mvc.UI.ViewComponentBuilderBase`2.ToHtmlString() +56   Telerik.Web.Mvc.{Dynamic}.GridBuilder`1[[TelerikMvcApplication2.Models.GetSalesTotalsForAllRegions_Result, TelerikMvcApplication2, Version=2012.1.214.0\, Culture=neutral\, PublicKeyToken=null\]\].IHtmlString.ToHtmlString() +31   System.Web.HttpUtility.HtmlEncode(Object value) +38   System.Web.WebPages.WebPageBase.Write(Object value) +64   ASP._Page_Views_Home_Index_cshtml.Execute() in t:\TelerikMvcApplication2\TelerikMvcApplication2\Views\Home\Index.cshtml:6   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81   System.Web.WebPages.StartPage.RunPage() +19   System.Web.WebPages.StartPage.ExecutePageHierarchy() +65   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260   System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343   System.Web.Mvc.Controller.ExecuteCore() +116   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970061   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Coordinator
Apr 18, 2012 at 10:01 PM
Edited Apr 18, 2012 at 10:02 PM

The NullReferenceException you are experiencing is because of this line in your Home/Index view:

".BindTo((IEnumerable) ViewBag.Data)"

remove that line and see if it works.

Apr 19, 2012 at 12:21 AM

Thank you, that does resolve the issue null isuue, however, 

  • CustomServerBinding does not get called initially, only when I refresh the grid
  • gridHelper.BuildGridModel(); gives me "The result of a query cannot be enumerated more than once.". 
  • Remember, I'm pulling data from a stored procedure-- var query = db.GetSalesTotalsForAllRegions(new DateTime(DateTime.Now.Year, 1, 1), DateTime.Now, "t").AsQueryable();

thanks again for your assistance.

Apr 19, 2012 at 1:29 AM

var query = db.GetSalesTotalsForAllRegions(new DateTime(DateTime.Now.Year, 1, 1), DateTime.Now, "t").ToList().AsQueryable();

obviously gets around the enumeration more than once issue, but it brings in a new issue.

gridHelper.BuildGridModel(); results in an exception

Mapping not found for types: TelerikMvcApplication2.Models.GetSalesTotalsForAllRegions_Result to TelerikMvcApplication2.Models.GetSalesTotalsForAllRegions_Result.Mapping not found for types: TelerikMvcApplication2.Models.GetSalesTotalsForAllRegions_Result to TelerikMvcApplication2.Models.GetSalesTotalsForAllRegions_Result.

thoughts?

 

[AutoMapperMappingException: Mapping not found for types: TelerikMvcApplication2.Models.GetSalesTotalsForAllRegions_Result to TelerikMvcApplication2.Models.GetSalesTotalsForAllRegions_Result.Mapping not found for types: TelerikMvcApplication2.Models.GetSalesTotalsForAllRegions_Result to TelerikMvcApplication2.Models.GetSalesTotalsForAllRegions_Result.]   TelerikMvcGridCustomBindingHelper.Guard.IsMapped(Type typeIn, Type typeOut) +153   TelerikMvcGridCustomBindingHelper.Guard.IsMapped() +150   TelerikMvcGridCustomBindingHelper.Mapper.GridModelMapper.AssertMappings() +206   TelerikMvcGridCustomBindingHelper.Mapper.GridModelMapper.Map(IEnumerable`1 sources) +72   TelerikMvcGridCustomBindingHelper.GridCustomBindingHelper`2.ConvertEntitiesToViewModels(IEnumerable`1 data) +108   TelerikMvcGridCustomBindingHelper.GridCustomBindingHelper`2.EnsureDataSourceIsProcessed() +1303   TelerikMvcGridCustomBindingHelper.GridCustomBindingHelperBase`2.BuildGridModel() +38   TelerikMvcApplication2.Controllers.HomeController.CustomServerBinding(GridCommand command) in t:\TelerikMvcApplication2\TelerikMvcApplication2\Controllers\HomeController.cs:33   lambda_method(Closure , ControllerBase , Object[] ) +162   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343   System.Web.Mvc.Controller.ExecuteCore() +116   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970061   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Coordinator
Apr 19, 2012 at 2:45 AM

This seems to be a problem in our helperbecause in this case is definitely not necessary to configure a mapping.

I'll investigate it and soon come back here...

Coordinator
Apr 19, 2012 at 2:13 PM
Edited Jun 29, 2012 at 12:22 AM

I think I managed to solve your problem, let's go;

  1. I'm sorry I made ​​a mistakeyou should not remove that line (".BindTo((IEnumerable) ViewBag.Data)") to work with server bindings, put it back!
  2. In order to TelerikMvcGrid server bindings to work properly the action and view should have the same names!
  3. So your data access code should live in the Home/Index action
  4. Configure your grid to bind to the Home/Index action
  5. Besides all this I found out a little issue in the helper, which has been corrected in the new version, released today.
  6. Some additional settings you will find in your fixed sample project, take it from here: http://hop.tl/wg7JeT7hA4t6r8Pd6A
  7. And don't forget to update the helper to the new version.

I believe that should be enough for you to continue, thank you for reporting this issue and if you need anything else let us know.

Apr 19, 2012 at 2:57 PM

works like a charm now. Thank you so much.