8

i have mvc 4 master page where i want to display user specific menu at the top of the page i am retrieving user data

@using Agenda_MVC.Engine
@{

    List<Format> ContactEvents = Agenda_MVC.Classes.Utility.GetContactEventFormats(User.Identity.Name);
}

but when i iterate through the list it throws null exception, on setting breakpoint the list is not null it has items.

@foreach (var ContactEvent in ContactEvents)
                {
                    <div class="@(ContactEvent.EventId == ViewContext.RouteData.Values["id"].ToString() ? "StaticMenuItemSelected" : "StaticMenuItem")">
                       @Html.ActionLink(ContactEvent.Name.Length > 15 ? ContactEvent.Name.Substring(0, 15) + "..." : ContactEvent.Name, "Agenda", "Portal", new { id = ContactEvent.EventId }, new { @title = ContactEvent.Name })
                    </div>    
                }

enter image description here

i dont know what i am doing wrong.


code for method is below i am retrieving it from a web service

public static List<Format> GetContactEventFormats(string ContactId)
{
    //List<Format> EventFormats = HttpContext.Current.Cache["EventFormats" + ContactId] as List<Format>;
    List<Format> EventFormats = HttpContext.Current.Session["EventFormats" + ContactId] as List<Format>;

    if (EventFormats == null)
    {
        EngineClient EngClient = Params.GetEngineClient();
        EventFormats = EngClient.GetContactEventFormats(ContactId);

        if (EventFormats != null && EventFormats.Count > 0)
        {
            //HttpContext.Current.Cache.Insert("EventFormats" + ContactId, EventFormats, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero);
            HttpContext.Current.Session["EventFormats" + ContactId] = EventFormats;
        }
    }

    return EventFormats == null ? new List<Format>() : EventFormats;
}
4
  • Post the code for ContactEvents. Maybe it is somehow lazy-initialized and returns null on first call, while returning non-null on second Commented Apr 24, 2013 at 12:58
  • Perhaps one of the 'ContactEvent' is 'null' and you get an exception because you call '.Name' and '.EventId'? Commented Apr 24, 2013 at 12:59
  • @mgttlinger none of the ContactEvent is null as the same code was working yesterday Commented Apr 24, 2013 at 13:01
  • @alex i have posted the code below Commented Apr 24, 2013 at 13:09

2 Answers 2

4

I assume ContactEvent.Name or EventId is null

You can check if both of these values are null in the debugger or by specifying a conditional before assigning the ContactEvent.Name to the ActionLink value.

<div class="@(ContactEvent.EventId == ViewContext.RouteData.Values["id"].ToString() ? "StaticMenuItemSelected" : "StaticMenuItem")">

                @if (ContactEvent.Name != null) {
                   @Html.ActionLink(ContactEvent.Name.Length > 15 ? ContactEvent.Name.Substring(0, 15) + "..." : ContactEvent.Name, "Agenda", "Portal", new { id = ContactEvent.EventId }, new { @title = ContactEvent.Name })
                }
                </div>   
Sign up to request clarification or add additional context in comments.

4 Comments

@user1799320 - also ensure ViewContext.RouteData.Values["id"] has a value.
none of the member of any object is null as its throwing exception before reaching that line, its throwing at the opening curly bracket.i can see all the values in the object and the object itself
@user1799320 - does ViewContext.RouteData.Values["id"] have a value?
ViewContext.RouteData.Values["id"] was null thanks i changed it to <div class="@(ViewContext.RouteData.Values["id"] != null && ContactEvent.EventId == ViewContext.RouteData.Values["id"].ToString() ? "StaticMenuItemSelected" : "StaticMenuItem")">
0

Take a look at the ContactEvent object in your List during debugging.

Perhaps (probably) one or more fields e.g: ContactEvent.EventId or ContactEvent.Name are null.

1 Comment

no they are not i can confirm that when i drill down the object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.