2014-01-06 3 views
0

안녕 얘들 아 내가 프로젝트를 실행하려고하면 개체를 저장하려고 할 때이 오류가 나타납니다. 보시다시피, 이것은 사용자가 다른 클래스에 값을 저장할 수있는 픽업 양식입니다. 거기에 가치가 저장됩니다 3 가지 클래스가 있습니다 .. 당신은 여전히 ​​C# 일에 신비입니다 참조하십시오. 나는 고객의 세부 사항, 픽업의 세부 사항을 가진 픽업 클래스 및 납품과 같은 것을 저장할 고객 클래스를 가지고있다. 다른 클래스 방문은 시간이 발생할 때만 저장됩니다. 그게 문제가 있다면 내가 당신에게 당신은 thePickup의 속성을 액세스하는오류 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은 아직 null이므로 NullReferenceException이 발생합니다. 어떤 점에서

당신은 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; 
} 

당신은

관련 문제