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

发布于 2024-09-05 05:23:57 字数 3907 浏览 7 评论 0原文

我的程序需要一些帮助。当我使用自定义 DayView 控件

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

根据错误代码,下面的“foreach”循环导致 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 控制文件中的 OnResolveAppointmentsResolveAppointment 函数。

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

有人有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感悟人生的甜 2024-09-12 05:23:57

DayView1_ResolveAppointments 显然是 DayView1 控件的 ResolveAppointments 事件的事件处理程序。如果 For Each 抛出异常,则意味着 appointments 当时是 Nothing,而不是您期望的空列表就这样吧。添加

If appointments Is Nothing Then
    Return
End If

For Each 循环之前。

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.

第七度阳光i 2024-09-12 05:23:57

您的 Calendar.DayView 控件似乎在其对 OnPaint 的重写中调用了 OnResolveAppointments 函数。我建议你检查那里的代码。

同时,如果appointmentsNothing,您可能可以跳过For Each

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文