简体   繁体   English

错误System.NullReferenceException

[英]Error System.NullReferenceException

hi guys when ever i try to execute the project it appears this error when i am trying to store the objects. 嗨,大家好,当我尝试执行项目时,当我尝试存储对象时就会出现此错误。 as you can see this is the pickup form which will allow user to store the value in different classes. 如您所见,这是取货表格,允许用户将值存储在不同的类中。 there are 3 different class the value will be store at.. you see am still a newbess at the c# thing. 值将存储在3个不同的类中。.您会发现c#仍然是新手。 i have customer class which will store the details of the customer, pickup class with details of the pickup and same with delivery. 我有客户类别,该类别将存储客户的详细信息,取件类别以及取件的详细信息,以及交货信息。 and with the other class Visit will only store when the time is occured. 与其他类一起使用,Visitor将仅在发生时间时存储。 i don't know if that is the problem.. let me know if you want to see more code.. thank you 我不知道这是否是问题..让我知道您是否想查看更多代码..谢谢

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

        }
    }
}

You're accessing properties of thePickup , but at no point have you assigned an object to the variable. 您正在访问thePickup属性,但thePickup没有为该变量分配对象。 So thePickup is still null , which is why you get the NullReferenceException. 所以thePickup仍然为null ,这就是为什么您得到NullReferenceException的原因。

At some point you need to instatiate a Pickup object and assign it to thePickup like so: 在某些时候,您需要实例化一个Pickup对象并将其分配给thePickup如下所示:

thePickup = new Pickup();

You should probably do this in either the constructor or the PickupForm_Load event handler. 您可能应该在构造函数或PickupForm_Load事件处理程序中执行此操作。

The same goes for theVisit and theDelivery . theVisittheDelivery

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

You need to initialize respective properties 您需要初始化各自的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM