This is a migrated thread and some comments may be shown as answers.

System.NullReferenceException

0 Answers 44 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Vinicius
Top achievements
Rank 1
Vinicius asked on 07 Jul 2010, 12:26 PM
ASP.NET MVC 2

I'm getting the following error:

[NullReferenceException: Referência de objeto não definida para uma instância de um objeto.]
Telerik.Web.Mvc.Infrastructure.Implementation.AuthorizeAttributeCache.GetAuthorizeAttributes(RequestContext requestContext, String controllerName, String actionName) +230
Telerik.Web.Mvc.Infrastructure.Implementation.ControllerAuthorization.IsAccessibleToUser(RequestContext requestContext, String controllerName, String actionName) +171
Telerik.Web.Mvc.Infrastructure.Implementation.NavigationItemAuthorization.IsAccessibleToUser(RequestContext requestContext, INavigatable navigationItem) +269
Telerik.Web.Mvc.UI.<>c__DisplayClass2`1.<IsAccessible>b__1(T item) +76
System.Linq.Enumerable.Any(IEnumerable`1 source, Func`2 predicate) +159
Telerik.Web.Mvc.UI.NavigatableExtensions.IsAccessible(IEnumerable`1 items, INavigationItemAuthorization authorization, ViewContext viewContext) +321
Telerik.Web.Mvc.UI.Menu.WriteItem(MenuItem item, IHtmlNode parentTag, IMenuHtmlBuilder builder) +443
Telerik.Web.Mvc.UI.<>c__DisplayClass4.<WriteHtml>b__3(MenuItem item) +58
Telerik.Web.Mvc.Extensions.EnumerableExtensions.Each(IEnumerable`1 instance, Action`1 action) +230
Telerik.Web.Mvc.UI.Menu.WriteHtml(HtmlTextWriter writer) +430
Telerik.Web.Mvc.UI.ViewComponentBase.Render() +147
Telerik.Web.Mvc.UI.ViewComponentBuilderBase`2.Render() +55
Telerik.Web.Mvc.UI.ViewComponentBuilderBase`2.ToString() +28
Igreja.Web.MenuHelpers.CreateMenu(HtmlHelper html) in C:\Projetos\IgrejaOnline\Igreja\Igreja.Web\Helpers\MenuHelpers.cs:47
ASP.views_shared_master_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Projetos\IgrejaOnline\Igreja\Igreja.Web\Views\Shared\Master.master:48
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266


Here is my code:

using System; 
using System.Web; 
using System.Web.Mvc; 
using Telerik.Web.Mvc.UI; 
using Igreja.Data; 
using System.Collections.Generic; 
using System.Linq; 
 
namespace Igreja.Web 
    public static class MenuHelpers 
    { 
        public static string CreateMenu(this HtmlHelper html) 
        { 
            if (SessionHelper.Menu == null
            { 
                MenuBuilder menu = html.Telerik().Menu(); 
                menu = menu.Name("menu"); 
 
                var repository = RepositoryFactory<Pagina>.Get() as IRepositoryPagina; 
                var paginas = repository.GetAll(); 
 
                IList<Pagina> raizes = repository.RaizesMenu(); 
                foreach (Pagina pagina in raizes) 
                { 
                    if (Seguranca.VerificarPermissao(SessionHelper.IdUsuario, pagina.Id)) 
                    { 
                        if (string.IsNullOrEmpty(pagina.Action)) 
                        { 
                            menu = menu.Items(item => item.Add() 
                                                          .Text(pagina.Nome) 
                                                          .AddMenuItems(SessionHelper.IdUsuario,  
                                                                        pagina.Hierarquia)); 
                        } 
                        else 
                        { 
                            menu = menu.Items(item => item.Add() 
                                                          .Action(pagina.Action, 
                                                                  pagina.Controller) 
                                                          .Text(pagina.Nome) 
                                                          .AddMenuItems(SessionHelper.IdUsuario, 
                                                                        pagina.Hierarquia)); 
                        } 
                    } 
                } 
 
                SessionHelper.Menu = menu.ToString(); 
            } 
 
            return SessionHelper.Menu; 
        } 
 
        public static void AddMenuItems(this MenuItemBuilder itemBuilder, int idUsuario, string hierarquia) 
        { 
            var repository = RepositoryFactory<Pagina>.Get() as IRepositoryPagina; 
            var paginas = repository.GetAll(); 
 
            IList<Pagina> subPaginas = repository.SubPaginas(hierarquia); 
            foreach (Pagina pagina in subPaginas) 
            { 
                if (Seguranca.VerificarPermissao(idUsuario, pagina.Id)) 
                { 
                    if (string.IsNullOrEmpty(pagina.Action)) 
                    { 
                        itemBuilder = itemBuilder.Items(item => item.Add() 
                                                                    .Text(pagina.Nome) 
                                                                    .AddMenuItems(idUsuario,  
                                                                                  pagina.Hierarquia)); 
                    } 
                    else 
                    { 
                        itemBuilder = itemBuilder.Items(item => item.Add() 
                                                                    .Action(pagina.Action,  
                                                                            pagina.Controller) 
                                                                    .Text(pagina.Nome) 
                                                                    .AddMenuItems(idUsuario,  
                                                                                  pagina.Hierarquia)); 
                    } 
                } 
            } 
        } 
    } 
 
 
Tags
Menu
Asked by
Vinicius
Top achievements
Rank 1
Share this question
or