2014-01-06 53 views
0

嗨,大家好,當我嘗試執行該項目時,出現此錯誤,當我試圖存儲對象。正如你所看到的,這是允許用戶將值存儲在不同類中的提取表單。有3個不同類的價值將存儲在..你看我仍然是一個新手在C#的東西。我有客戶類,它將存儲客戶的細節,皮卡類與皮卡的細節和交付相同。而其他課程只有在發生時纔會存儲。我不知道如果是這樣的問題..讓我知道,如果你想看到更多的代碼..謝謝錯誤System.NullReferenceException

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 


namespace coursework2 
{ 

    public partial class PickupForm : Form 
    { 
     private MainForm mainform; 
     private Pickup thePickup; 
     private Delivery theDelivery; 
     private Visit theVisit; 


     public Pickup pickup 
     { 
      get { return thePickup;} 
      set { thePickup = value;} 
     } 
     public Delivery delivery 
     { 
      get { return theDelivery; } 
      set { theDelivery = value; } 
     } 

     public Visit visit 
     { 
      get { return theVisit; } 
      set { theVisit = value; } 

     } 

     public PickupForm() 
     { 
      InitializeComponent(); 
     } 

     /*public MainForm ParentForm 
     { 
      get { return mainform; } 
      set { mainform = value; } 
     }*/ 

     private void PickupForm_Load(object sender, EventArgs e) 
     { 
      if (thePickup != null) 
      { 
       textCname.Text = thePickup.PickupName; 
       textAddress.Text = thePickup.PickupAddress; 
       textDate.Text = theVisit.DateTime.ToString(); 
       textDname.Text = theDelivery.DeliveryName; 
       textDaddress.Text = theDelivery.DeliveryAddress; 
      } 
     } 

     private void btnReturn_Click(object sender, EventArgs e) 
     { 

      this.Close(); 
      /*mainform.Show();*/ 
     } 


     private void btnClear_Click(object sender, EventArgs e) 
     { 
      textCname.Clear(); 
      textAddress.Clear(); 
      textDate.Clear(); 
      textDname.Clear(); 
      textDaddress.Clear(); 
     } 

     private void btnPickup_Click(object sender, EventArgs e) 
     { 
      thePickup.PickupName = textCname.Text; //error occurs from this line 
      thePickup.PickupName = textAddress.Text; 
      theVisit.DateTime = DateTime.Parse(textDate.Text); 
      theDelivery.DeliveryName = textDname.Text; 
      theDelivery.DeliveryAddress = textDaddress.Text; 



      this.Close(); 
     } 




     private void textDate_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void textCname_TextChanged(object sender, EventArgs e) 
     { 

     } 
    } 
} 
+0

屬性名應當按照約定大寫字母開頭。 –

回答

1

你所訪問的thePickup性能,但在任何時候,有你分配一個對象到變量。所以thePickup仍然是null,這就是爲什麼你得到NullReferenceException。

在某些時候,你需要一個instatiate對象Pickup並將其分配給thePickup像這樣:

thePickup = new Pickup(); 

你或許應該這樣做無論是在構造函數或PickupForm_Load事件處理程序。

theVisittheDelivery也是如此。

+0

或者確保屬性是從創建並顯示錶單的代碼中設置的。 –

0
private void PickupForm_Load(object sender, EventArgs e) 
{ 
    if(thePickup == null) 
    { 
     thePickup = new Pickup(); 
    } 

    if(theDelivery == null) 
    { 
     theDelivery = new Delivery(); 
    } 

    if(theVisit == null) 
    { 
     theVisit = new Visit(); 
    } 

    textCname.Text = thePickup.PickupName; 
    textAddress.Text = thePickup.PickupAddress; 
    textDate.Text = theVisit.DateTime.ToString(); 
    textDname.Text = theDelivery.DeliveryName; 
    textDaddress.Text = theDelivery.DeliveryAddress; 
} 

需要初始化各自的屬性

相關問題