“对象引用未设置为对象的实例”添加到列表时

时间:2010-06-10 03:57:53

标签: vb.net exception nullreferenceexception object-reference

我的程序需要一些帮助。当我使用自定义DayView control运行我的VB.NET程序时出现此错误。

  

**************例外文字**************   System.NullReferenceException:未将对象引用设置为对象的实例。      at SeaCow.Main.DayView1_ResolveAppointments(Object sender,ResolveAppointmentsEventArgs args)在C:\ Users \ Daniel \ My Programs \ Visual Basic \ SeaCow \ SeaCow \ SeaCow \ Main.vb:第120行      在Calendar.DayView.OnResolveAppointments(ResolveAppointmentsEventArgs args)      在Calendar.DayView.OnPaint(PaintEventArgs e)      在System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,Int16 layer)      在System.Windows.Forms.Control.WmPaint(消息& m)      在System.Windows.Forms.Control.WndProc(消息& m)      在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

根据错误代码,下面的'for each'循环导致NullReferenceException错误。默认情况下,“约会”列表分配给任何内容,我找不到调用ResolveAppointments函数的位置。

    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

以下是DayView.cs控制文件中的 OnResolveAppointments ResolveAppointment 函数。

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);
        }
    }

此外,还有 OnPaint 方法

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);
    }

有人有任何建议吗?

2 个答案:

答案 0 :(得分:1)

DayView1_ResolveAppointments显然是ResolveAppointments控件的DayView1事件的事件处理程序。如果For Each抛出异常,则表示此时appointmentsNothing,而不是空预期列表,正如您所期望的那样。添加

If appointments Is Nothing Then
    Return
End If

For Each循环之前。

答案 1 :(得分:0)

您的Calendar.DayView控件似乎在覆盖OnResolveAppointments的范围内调用OnPaint函数。我建议你检查那里的代码。

与此同时,如果For Eachappointments,您可能会跳过Nothing