0

我需要一些幫助來使用我的程序。當我使用自定義的DayView control運行我的VB.NET程序時,出現此錯誤。「對象引用未設置爲對象的實例」當添加到列表中時

**************異常文本************** System.NullReferenceException:未將對象引用設置爲目的。 at 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(消息&米) 在系統。 Windows.Forms.Control.WndProc(消息&米) 在System.Windows.Forms.NativeWindow.Callback(IntPtr的的HWND,MSG的Int32,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 

這裏是OnResolveAppointments並從DayView.cs控制文件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); 
    } 

任何人有什麼建議?

+0

那些名稱以'm_'開頭的變量是什麼?對我來說,這是一個通常用於表示成員字段(不是局部變量)的約定;對我來說這太奇怪了! – 2010-06-20 01:23:05

+0

幾乎所有的'NullReferenceException'都是一樣的。請參閱「[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」的一些提示。 – 2014-04-06 20:45:50

回答

1

DayView1_ResolveAppointments顯然是DayView1控件的ResolveAppointments事件的事件處理程序。如果For Each正在拋出異常,那麼這意味着appointments在當時是Nothing,而不是像您期望的那樣是空列表。添加

If appointments Is Nothing Then 
    Return 
End If 

之前的For Each循環。

0

看起來您的Calendar.DayView控件在其覆蓋OnPaint的範圍內調用OnResolveAppointments函數。我建議你檢查那裏的代碼。

與此同時,如果appointmentsNothing,則可能跳過For Each

相關問題