Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom Krzesinski Jr 3 posts 73 karma points
    Dec 17, 2015 @ 20:15
    Tom Krzesinski Jr
    0

    Object reference not set to an instance of an object.

    Upgraded to a new host and umbraco 7 now the only thing broken in my events page. When you click on "Read full article" from here you will see the Object reference error. It should look like this page here. Not sure how to fix the problem any help is appreciated. Thanks, Tommy

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Dec 19, 2015 @ 08:00
    Kevin Jump
    0

    HI Tom,

    we probibly need a little more detail, to help you get to the bottom of this one.

    when you go to the page, the error looks like its occurring in OWBEvent.FilterEvents() this is probably code from an additional class library attached to your site. Do you have the code for this ? and can you maybe post the code for that function ?

    if you do have the code, i would start by looking at places where it maybe assumes that the things it is working with are set (i.e not checking for null).

    if it's old code i often end up littering the code with LogHelper.Info calls (for example:)

    LogHelper.Info<OWBEvent>("Filtering Events: {0}", ()=> somevalue); 
    

    That write things out to the umbraco trace log (in app_data/logs) as that can help you to quickly workout what is going on inside the function. even if you just litter the function with a couple of lines saying

    LogHelper.Info<OWBEvent>("Filtering Events 1:"); 
    ... somecode...
    LogHelper.Info<OWBEvent>("Filtering Events 2:"); 
    ... etc
    

    you will be able to see where about it is breaking, as the logging will stop when the error is thrown...

  • Tom Krzesinski Jr 3 posts 73 karma points
    Jan 13, 2016 @ 16:18
    Tom Krzesinski Jr
    0

    Hello and thank you sorry I am pretty green with this stuff. I was able to get some more detail. Turned debugging to true and now it throws this error. Thanks again, Tommy

    Line 167: var eventStart = eventNode.PropertiesAsList.Where(p => p.Alias == "startDateTime").FirstOrDefault().Value;

    Bellow is a copy of the EventCalandar.ascx.xs file.

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; using System.Xml.XPath; //using DreamSongs.MongoRepository; //using OWB.Models; using umbraco; using umbraco.cms.businesslogic.media; using umbraco.cms.businesslogic.member; using umbraco.cms.businesslogic.web; using umbraco.cms.helpers; using umbraco.cms.presentation; using umbraco.NodeFactory;

    public partial class usercontrols_EventsCalendar : System.Web.UI.UserControl { private List

    private List<OWBEvent> OWBEventsList;
    public int CalendarHeight { get; set; }
    public int CalendarWidth { get; set; }
    public List<OWBEvent> OWBEvents { get; set; }
    public DateTime SelectedDate { get; set; }
    public DateTime? newDate;
    
    public EventHandler OnCalendarDateChanged;
    
    protected void Page_Load(object sender, EventArgs e)
    {
    
        if (!this.Page.IsPostBack)
        {
            this.eventsCalendar.Height = CalendarHeight;
            this.eventsCalendar.Width = CalendarWidth;
        }
    
        FillCalender();
    }
    
    private void FillCalender()
    {
        EventDatesList = GetEventsDateList();
    
    }
    
    protected void eventsCalendar_DayRender(object sender, DayRenderEventArgs e)
    {
        DateTime renderDay = e.Day.Date;
        if (renderDay.ToShortDateString() == DateTime.Now.ToShortDateString())
        {
            e.Cell.Style.Add("background", "url(/images/todayNoEvent.png) top right");
            e.Cell.Style.Add("color", "#fff");
        }
        else
        {
            e.Cell.Style.Add("background", "url(/images/any-day.png)");
        }
        LiteralControl hrefLiteral = (LiteralControl)e.Cell.Controls[0];
    
        e.Day.IsSelectable = false;
        foreach (DateTime cEvent in EventDatesList)
        {
            string cellToolTip = " ";
    
            if (renderDay.ToShortDateString() == cEvent.Date.ToShortDateString())
            {
                List<string> EventNamesList = OWBEventsList.OrderBy(o => o.StartDateTime).Where(o => o.StartDateTime.ToShortDateString() == renderDay.ToShortDateString()).Select(o => o.EventName).ToList();
                e.Cell.Controls.RemoveAt(0);
                System.Web.UI.HtmlControls.HtmlAnchor href = new System.Web.UI.HtmlControls.HtmlAnchor();
                StringBuilder sb = new StringBuilder();
                foreach (string eventName in EventNamesList)
                {
                    sb.Append(eventName + "\r\n");
                }
                href.Title = sb.ToString();
                cellToolTip = sb.ToString();
                href.InnerText = e.Day.Date.Day.ToString();
                href.HRef = e.SelectUrl;
                e.Cell.Controls.Add(href);
    
                e.Day.IsSelectable = true;
                if (renderDay.ToShortDateString() == DateTime.Now.ToShortDateString())
                {
                    e.Cell.Style.Add("background", "url(/images/today.png) top right");
                }
                else
                {
                    e.Cell.Style.Add("background", "url(/images/event.png) top right");
                }
    
                e.Cell.ToolTip = cellToolTip;
            }
    
    
    
        }
    }
    
    protected void eventsCalendar_SelectionChanged(object sender, EventArgs e)
    {
        if (Session["dateClicked"] == null)
        {
            Session.Add("dateClicked", true);
        }
        else
        {
            Session["dateClicked"] = true;
        }
        DateTime selectDay = eventsCalendar.SelectedDate;
        if (Session["monthSelected"] == null)
        {
            Session.Add("monthSelected", false);
        }
        else
        {
            Session["monthSelected"] = false;
        }
    
        this.calendarSelectedDayHidden.Value = selectDay.ToShortDateString();
        this.SelectedDate = eventsCalendar.SelectedDate;
        OWBEvent owbEvent = new OWBEvent();
        owbEvent.DateFromCalendar = this.SelectedDate;
        this.OWBEvents = owbEvent.GetOWBEvents();
    
        if (OnCalendarDateChanged != null)
            OnCalendarDateChanged(this, e);
    
        string url = Request.ServerVariables["URL"];
    
        DateTime? selDay = this.SelectedDate;
    
        if (Session["selectDay"] == null)
        {
    
            Session.Add("selectDay", selDay);
        }
        else
        {
            Session["selectDay"] = selDay;
        }
    
        if (!url.ToLower().Contains("events.aspx"))
        {
    
            Response.Redirect("/Events.aspx", true);
        }
    }
    
    private List<DateTime> GetEventsDateList()
    {
        EventDatesList = new List<DateTime>();
    
        OWBEvent owbEvent = new OWBEvent();
    
        List<Node> AllEvents = owbEvent.GetAllEvents();
        OWBEventsList = owbEvent.GetAllOWBEvents();
        foreach (var eventNode in AllEvents)
        {
    
            var eventStart = eventNode.PropertiesAsList.Where(p => p.Alias == "startDateTime").FirstOrDefault().Value;
            var startDate = Convert.ToDateTime(eventStart); 
    
            var eventEnd = eventNode.PropertiesAsList.Where(p => p.Alias == "endDateTime").FirstOrDefault().Value;
    
            if (!string.IsNullOrEmpty(eventEnd))
            {
                var endDate = Convert.ToDateTime(eventEnd);
                if (endDate.Date != startDate.Date)
                {
                    int dayDiff = (endDate - startDate).Days;
    
                    for (int i = 0; i <= dayDiff; i++)
                    {
                        EventDatesList.Add(startDate.AddDays(i));
                    }
    
                    EventDatesList.Add(endDate);
                }
                else
                {
                    EventDatesList.Add(startDate);
                }
            }
            else
            {
                EventDatesList.Add(startDate);
            }
    
        }
    
        return EventDatesList;
    }
    protected void eventsCalendar_MonthChanged(object sender, MonthChangedEventArgs e)
    {
        if (Session["dateClicked"] == null)
        {
            Session.Add("dateClicked", false);
        }
        else
        {
            Session["dateClicked"] = false;
        }
    
        if (Session["monthSelected"] == null)
        {
            Session.Add("monthSelected", true);
        }
        else
        {
            Session["monthSelected"] = true;
        }
    
    }
    protected void eventsCalendar_PreRender(object sender, EventArgs e)
    {
        Calendar cal = (Calendar)sender;
        if (!this.Page.IsPostBack)
        {
            string requestedDateString = ((DateTime?)Session["selectDay"]).HasValue ? ((DateTime?)Session["selectDay"]).Value.ToShortDateString() : string.Empty;
            if (!string.IsNullOrEmpty(requestedDateString))
            {
    
                DateTime myDate = Convert.ToDateTime(requestedDateString);
    
                cal.TodaysDate = myDate;
                cal.VisibleDate = new DateTime(cal.TodaysDate.Year, myDate.Month, 1);
            }
        }
    }
    

    } using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; using System.Xml.XPath; //using DreamSongs.MongoRepository; //using OWB.Models; using umbraco; using umbraco.cms.businesslogic.media; using umbraco.cms.businesslogic.member; using umbraco.cms.businesslogic.web; using umbraco.cms.helpers; using umbraco.cms.presentation; using umbraco.NodeFactory;

    public partial class usercontrols_EventsCalendar : System.Web.UI.UserControl { private List

    private List<OWBEvent> OWBEventsList;
    public int CalendarHeight { get; set; }
    public int CalendarWidth { get; set; }
    public List<OWBEvent> OWBEvents { get; set; }
    public DateTime SelectedDate { get; set; }
    public DateTime? newDate;
    
    public EventHandler OnCalendarDateChanged;
    
    protected void Page_Load(object sender, EventArgs e)
    {
    
        if (!this.Page.IsPostBack)
        {
            this.eventsCalendar.Height = CalendarHeight;
            this.eventsCalendar.Width = CalendarWidth;
        }
    
        FillCalender();
    }
    
    private void FillCalender()
    {
        EventDatesList = GetEventsDateList();
    
    }
    
    protected void eventsCalendar_DayRender(object sender, DayRenderEventArgs e)
    {
        DateTime renderDay = e.Day.Date;
        if (renderDay.ToShortDateString() == DateTime.Now.ToShortDateString())
        {
            e.Cell.Style.Add("background", "url(/images/todayNoEvent.png) top right");
            e.Cell.Style.Add("color", "#fff");
        }
        else
        {
            e.Cell.Style.Add("background", "url(/images/any-day.png)");
        }
        LiteralControl hrefLiteral = (LiteralControl)e.Cell.Controls[0];
    
        e.Day.IsSelectable = false;
        foreach (DateTime cEvent in EventDatesList)
        {
            string cellToolTip = " ";
    
            if (renderDay.ToShortDateString() == cEvent.Date.ToShortDateString())
            {
                List<string> EventNamesList = OWBEventsList.OrderBy(o => o.StartDateTime).Where(o => o.StartDateTime.ToShortDateString() == renderDay.ToShortDateString()).Select(o => o.EventName).ToList();
                e.Cell.Controls.RemoveAt(0);
                System.Web.UI.HtmlControls.HtmlAnchor href = new System.Web.UI.HtmlControls.HtmlAnchor();
                StringBuilder sb = new StringBuilder();
                foreach (string eventName in EventNamesList)
                {
                    sb.Append(eventName + "\r\n");
                }
                href.Title = sb.ToString();
                cellToolTip = sb.ToString();
                href.InnerText = e.Day.Date.Day.ToString();
                href.HRef = e.SelectUrl;
                e.Cell.Controls.Add(href);
    
                e.Day.IsSelectable = true;
                if (renderDay.ToShortDateString() == DateTime.Now.ToShortDateString())
                {
                    e.Cell.Style.Add("background", "url(/images/today.png) top right");
                }
                else
                {
                    e.Cell.Style.Add("background", "url(/images/event.png) top right");
                }
    
                e.Cell.ToolTip = cellToolTip;
            }
    
    
    
        }
    }
    
    protected void eventsCalendar_SelectionChanged(object sender, EventArgs e)
    {
        if (Session["dateClicked"] == null)
        {
            Session.Add("dateClicked", true);
        }
        else
        {
            Session["dateClicked"] = true;
        }
        DateTime selectDay = eventsCalendar.SelectedDate;
        if (Session["monthSelected"] == null)
        {
            Session.Add("monthSelected", false);
        }
        else
        {
            Session["monthSelected"] = false;
        }
    
        this.calendarSelectedDayHidden.Value = selectDay.ToShortDateString();
        this.SelectedDate = eventsCalendar.SelectedDate;
        OWBEvent owbEvent = new OWBEvent();
        owbEvent.DateFromCalendar = this.SelectedDate;
        this.OWBEvents = owbEvent.GetOWBEvents();
    
        if (OnCalendarDateChanged != null)
            OnCalendarDateChanged(this, e);
    
        string url = Request.ServerVariables["URL"];
    
        DateTime? selDay = this.SelectedDate;
    
        if (Session["selectDay"] == null)
        {
    
            Session.Add("selectDay", selDay);
        }
        else
        {
            Session["selectDay"] = selDay;
        }
    
        if (!url.ToLower().Contains("events.aspx"))
        {
    
            Response.Redirect("/Events.aspx", true);
        }
    }
    
    private List<DateTime> GetEventsDateList()
    {
        EventDatesList = new List<DateTime>();
    
        OWBEvent owbEvent = new OWBEvent();
    
        List<Node> AllEvents = owbEvent.GetAllEvents();
        OWBEventsList = owbEvent.GetAllOWBEvents();
        foreach (var eventNode in AllEvents)
        {
    
            var eventStart = eventNode.PropertiesAsList.Where(p => p.Alias == "startDateTime").FirstOrDefault().Value;
            var startDate = Convert.ToDateTime(eventStart); 
    
            var eventEnd = eventNode.PropertiesAsList.Where(p => p.Alias == "endDateTime").FirstOrDefault().Value;
    
            if (!string.IsNullOrEmpty(eventEnd))
            {
                var endDate = Convert.ToDateTime(eventEnd);
                if (endDate.Date != startDate.Date)
                {
                    int dayDiff = (endDate - startDate).Days;
    
                    for (int i = 0; i <= dayDiff; i++)
                    {
                        EventDatesList.Add(startDate.AddDays(i));
                    }
    
                    EventDatesList.Add(endDate);
                }
                else
                {
                    EventDatesList.Add(startDate);
                }
            }
            else
            {
                EventDatesList.Add(startDate);
            }
    
        }
    
        return EventDatesList;
    }
    protected void eventsCalendar_MonthChanged(object sender, MonthChangedEventArgs e)
    {
        if (Session["dateClicked"] == null)
        {
            Session.Add("dateClicked", false);
        }
        else
        {
            Session["dateClicked"] = false;
        }
    
        if (Session["monthSelected"] == null)
        {
            Session.Add("monthSelected", true);
        }
        else
        {
            Session["monthSelected"] = true;
        }
    
    }
    protected void eventsCalendar_PreRender(object sender, EventArgs e)
    {
        Calendar cal = (Calendar)sender;
        if (!this.Page.IsPostBack)
        {
            string requestedDateString = ((DateTime?)Session["selectDay"]).HasValue ? ((DateTime?)Session["selectDay"]).Value.ToShortDateString() : string.Empty;
            if (!string.IsNullOrEmpty(requestedDateString))
            {
    
                DateTime myDate = Convert.ToDateTime(requestedDateString);
    
                cal.TodaysDate = myDate;
                cal.VisibleDate = new DateTime(cal.TodaysDate.Year, myDate.Month, 1);
            }
        }
    }
    

    }

Please Sign in or register to post replies

Write your reply to:

Draft