C# 错误System.NullReferenceException

C# 错误System.NullReferenceException,c#,nullreferenceexception,C#,Nullreferenceexception,大家好,当我尝试执行项目时,当我尝试存储对象时,会出现此错误。正如您所看到的,这是一个拾取表单,它允许用户将值存储在不同的类中。该值将存储在3个不同的类中。。你看,我还是c#东西的新手。我有一个customer类,它将存储客户的详细信息,皮卡类包含皮卡的详细信息,与交货相同。与其他类一起,访问将仅在时间发生时存储。我不知道这是否是问题所在。。如果您想查看更多代码,请告诉我。。多谢各位 using System; using System.Collections.Generic; using Sy

大家好,当我尝试执行项目时,当我尝试存储对象时,会出现此错误。正如您所看到的,这是一个拾取表单,它允许用户将值存储在不同的类中。该值将存储在3个不同的类中。。你看,我还是c#东西的新手。我有一个customer类,它将存储客户的详细信息,皮卡类包含皮卡的详细信息,与交货相同。与其他类一起,访问将仅在时间发生时存储。我不知道这是否是问题所在。。如果您想查看更多代码,请告诉我。。多谢各位

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)
        {

        }
    }
}

您正在访问pickup的属性,但没有为变量指定对象。因此,
pickup
仍然是
null
,这就是为什么会出现NullReferenceException

在某些时候,您需要建立一个
拾取
对象,并将其分配给Pickup,如下所示:

thePickup = new Pickup();
您可能应该在构造函数或
PickupForm\u Load
事件处理程序中执行此操作

这同样适用于
访问
交付

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

您需要初始化相应的属性

或确保属性是根据创建和显示表单的代码设置的。.按照惯例,属性名称应以大写字母开头。