Running into a null reference issue with the treeview control

Dec 30, 2012 at 12:46 AM
Here is the stack trace. Any help appreciated
System.Web.HttpException (0x80004005): Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at MVCControlsToolkit.Controls.TreeViewHelpers.TreeViewRec[VM,T](StringBuilder sb, StringBuilder sbInit, Boolean editMode, HtmlHelper`1 htmlHelper, RenderInfo`1 renderInfo, Func`2 collectionName, ExternalContainerType itemContainer, String rootClass, Object[] itemTemplates, Func`3 itemTemplateSelector, Func`2 itemClassSelector, Func`3 itemStatus, Single opacity, Boolean canMove, Int32 level, Int32 totalCount, String fatherName, String root_id)
   at MVCControlsToolkit.Controls.TreeViewHelpers.TreeViewTop[VM,T](StringBuilder sb, Boolean editMode, HtmlHelper`1 htmlHelper, RenderInfo`1 renderInfo, Func`2 collectionName, ExternalContainerType itemContainer, String rootClass, Object[] itemTemplates, Func`3 itemTemplateSelector, Func`2 itemClassSelector, Func`3 itemStatus, Single opacity, Boolean canMove, Boolean canAdd, TreeViewOptions treeViewOptions)
   at MVCControlsToolkit.Controls.TreeViewHelpers.TreeViewFor[VM,TItem](HtmlHelper`1 htmlHelper, RenderInfo`1 renderInfo, Func`2 collectionName, ExternalContainerType itemContainer, String rootClassDisplay, Object[] displayTemplates, Func`3 itemTemplateSelectorDisplay, String rootClassEdit, Object[] editTemplates, Func`3 itemTemplateSelectorEdit, TreeViewMode mode, Func`2 itemClassSelector, Func`3 itemStatus, TreeViewOptions treeViewOptions)
   at ASP._Page_Views_Album__IndexedPhotoCatalogPartial_cshtml.Execute() in c:\suryas.com\Suryas.Web.UI\Views\Album\_IndexedPhotoCatalogPartial.cshtml:line 6
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)
   at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.<>c__DisplayClass4.<Wrap>b__3()
   at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap[TResult](Func`1 func)
   at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride)
   at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride)
   at System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage)
   at System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm)
   at System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter)
   at System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues)
   at ASP._Page_Views_Album_Index_cshtml.Execute() in c:\suryas.com\Suryas.Web.UI\Views\Album\Index.cshtml:line 15
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)
   at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
 
Here is my node class
 
  public class PhotoTreeNode : ISafeCreation
    {
        public PhotoTreeNode()
        {
            ChildNodes = new List<PhotoTreeNode>();
        }
 
 
        public DateTime Date
        {
            get;
            set;
        }
 
        public int Year
        {
            get;
            set;
        }
 
        public int Month
        {
            get;
            set;
        }
 
        /// <summary>
        /// 
        /// </summary>
        public int Day
        {
            get;
            set;
 
        }
 
        public int TotalPhotos
        {
 
            get;
            set;
        }
 
        public string DisplayText
        {
 
            get;
            set;
        }
 
        public string ActionLinkUrl
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public int NodeDepth
        {
 
            get;
            set;
        }
 
 
        public PhotoNodeType NodeType
        {
 
            get;
            set;
        }
 
        public List<PhotoTreeNode> ChildNodes
        {
            get;
            set;
        }
    }
Here is my VM 
 public class PhotoAlbumCatalogViewModel
    {
 
        /// <summary>
        /// 
        /// </summary>
        public List<PhotoTreeNode> Nodes
        {
 
            get;
            set;
        }
 
    }
}
Here is the razor view
@Html.TreeViewFor(
            m => m.Nodes,
            i => "Nodes",
            ExternalContainerType.span,
            null,
            new object[]
            {
                _S.L<PhotoTreeNode>(h => h.DisplayFor(m => m.DisplayText).ToString())
            },
            (x, y) => 0,
            null,
            null,
            null,
            TreeViewMode.Display,
            null,
            (x, y) => TreeViewItemStatus.Hide,
            null
        )
Coordinator
Jan 3, 2013 at 11:59 AM

The name of the collection of each node that contain the childre is ChildNodes, while the property of the viemodel that contains the tree is Node, so:

  • The first argument m => m.Nodes is OK
  • However, the second argument must be a function that returns the property containin the children of the current node being rendered, so you must substitute i => "Nodes" with i => "ChildNodes"

After this change,  everything should work!