0

I need some help with my program. I get this error when I run my VB.NET program with a custom DayView control.

************** Exception Text ************** System.NullReferenceException: Object reference not set to an instance of an object. at SeaCow.Main.DayView1_ResolveAppointments(Object sender, ResolveAppointmentsEventArgs args) in C:\Users\Daniel\My Programs\Visual Basic\SeaCow\SeaCow\SeaCow\Main.vb:line 120 at Calendar.DayView.OnResolveAppointments(ResolveAppointmentsEventArgs args) at Calendar.DayView.OnPaint(PaintEventArgs e) at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer) at System.Windows.Forms.Control.WmPaint(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

According to the error code, the 'for each' loop below is causing the NullReferenceException error. At default, the 'appointments' list is assigned to nothing and I can't find where the ResolveAppointments function is being called at.

    Private Sub DayView1_ResolveAppointments(ByVal sender As Object, ByVal args As Calendar.ResolveAppointmentsEventArgs) Handles DayView1.ResolveAppointments
    Dim m_Apps As New List(Of Calendar.Appointment)

    For Each m_App As Calendar.Appointment In appointments
        If (m_App.StartDate >= args.StartDate) AndAlso (m_App.StartDate <= args.EndDate) Then
            m_Apps.Add(m_App)
        End If
    Next

    args.Appointments = m_Apps
End Sub

Here is the OnResolveAppointments and ResolveAppointment functions from the DayView.cs control file.

public event EventHandler<ResolveAppointmentsEventArgs> OnResolveAppointments;



protected virtual void ResolveAppointments(ResolveAppointmentsEventArgs args)
    {
        System.Diagnostics.Debug.WriteLine("Resolve app");

        if (OnResolveAppointments != null)
            OnResolveAppointments(this, args);

        this.allDayEventsHeaderHeight = 0;

        // cache resolved appointments in hashtable by days.
        cachedAppointments.Clear();

        if ((selectedAppointmentIsNew) && (selectedAppointment != null))
        {
            if ((selectedAppointment.StartDate > args.StartDate) && (selectedAppointment.StartDate < args.EndDate))
            {
                args.Appointments.Add(selectedAppointment);
            }
        }

        foreach (Appointment appointment in args.Appointments)
        {
            int key = -1;
            AppointmentList list;

            if (appointment.StartDate.Day == appointment.EndDate.Day && appointment.AllDayEvent == false)
            {
                key = appointment.StartDate.Day;
            }
            else
            {
                key = -1;
            }

            list = (AppointmentList)cachedAppointments[key];

            if (list == null)
            {
                list = new AppointmentList();
                cachedAppointments[key] = list;
            }

            list.Add(appointment);
        }
    }

Also, here is the OnPaint method

protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        // resolve appointments on visible date range.
        ResolveAppointmentsEventArgs args = new ResolveAppointmentsEventArgs(this.StartDate, this.StartDate.AddDays(daysToShow));
        ResolveAppointments(args);

        using (SolidBrush backBrush = new SolidBrush(renderer.BackColor))
            e.Graphics.FillRectangle(backBrush, this.ClientRectangle);

        // Visible Rectangle
        Rectangle rectangle = new Rectangle(0, 0, this.Width - VScrollBarWith, this.Height);

        DrawDays(ref e, rectangle);

        DrawHourLabels(ref e, rectangle);

        DrawDayHeaders(ref e, rectangle);
    }

Anyone have any suggestions?

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Daniel Harris
  • 1,545
  • 10
  • 42
  • 60
  • What's with those variables with names starting `m_`? To me that's a convention normally used to indicate member fields (not local variables); that looks so weird to me! – Dan Tao Jun 20 '10 at 01:23
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Apr 06 '14 at 20:45

2 Answers2

1

DayView1_ResolveAppointments is clearly an event handler for the ResolveAppointments event of the DayView1 control. If the For Each is throwing the exception, then it means that appointments is Nothing at that time, and not an empty list, as you expect it to be. Add

If appointments Is Nothing Then
    Return
End If

before the For Each loop.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
0

It appears that your Calendar.DayView control calls the OnResolveAppointments function within its override of OnPaint. I would suggest you examine the code there.

In the meantime, you could probably just skip over the For Each if appointments is Nothing.

Adam Robinson
  • 171,726
  • 31
  • 271
  • 330