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

namespace WindowsFormsApplication1
{
public partial class Form5 : Form
{
     private Pickups thePickups;
    //The pickups object being created/edited


    public Pickups appointment
    {   //Property to allow access to thePickups
        get { return thePickups; }
        set { thePickups = value; }
    }


    public Form5()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        thePickups.custName = textBox1.Text;
        thePickups.custAddress= textBox2.Text;
        thePickups.arrival = textBox3.Text;
        thePickups.delAddress = textBox4.Text;
        thePickups.delName = textBox5.Text;


        this.Hide();
        //Hide the form
    }

    private void Form5_Load(object sender, EventArgs e)
    {



      if (thePickups != null)
        {
            textBox1.Text = thePickups.custName;
            textBox2.Text = thePickups.delAddress.ToString();
            textBox3.Text = thePickups.arrival.ToString();
        } 
    }
    }
}

我收到一条错误消息 - 对象引用未设置为对象的实例,这是我项目中的许多文件之一,但它是我遇到任何错误的第一个文件。我很难看到我在这里犯了错误,任何帮助将不胜感激。

4

3 回答 3

0

您需要确保在您创建的“约会”属性中设置了一些值。也可以在您的 button_click 事件中执行此操作。

private void button1_Click(object sender, EventArgs e)
{
    if(thePickups != null)
    {
        thePickups.custName = textBox1.Text;
        thePickups.custAddress= textBox2.Text;
        thePickups.arrival = textBox3.Text;
        thePickups.delAddress = textBox4.Text;
        thePickups.delName = textBox5.Text;


        this.Hide();
        //Hide the form
    }
}
于 2012-12-05T15:31:05.797 回答
0

是我失明还是我看不到一条线

thePickups = new Pickups();

任何地方?

顺便说一句,form.Hide 看起来也很狡猾。你真的确定你想要它但隐藏吗?但这不会解释您的例外情况。

于 2012-12-05T15:00:47.467 回答
0

您定义thePickups为;

 private Pickups thePickups;
//The pickups object being created/edited


public Pickups appointment
{   //Property to allow access to thePickups
    get { return thePickups; }
    set { thePickups = value; }
}

但永远不要设置为对象的实例。然后,您尝试检查此 ojbect 实例是否null存在。

if (thePickups != null)
{

} 
于 2012-12-05T15:01:31.083 回答