C# C表单输入数据错误,未设置为对象实例
我得到一个错误,说-Object引用没有设置为对象的实例,这是我项目中许多文件中的一个,但这是我遇到的第一个错误。我正在努力找出我在这里犯下的错误,任何帮助都将不胜感激。是我失明了还是我看不到一条线C# C表单输入数据错误,未设置为对象实例,c#,forms,C#,Forms,我得到一个错误,说-Object引用没有设置为对象的实例,这是我项目中许多文件中的一个,但这是我遇到的第一个错误。我正在努力找出我在这里犯下的错误,任何帮助都将不胜感激。是我失明了还是我看不到一条线 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.
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();
}
}
}
}
哪里
另外,form.Hide看起来也很狡猾。你真的确定你想把它藏起来吗?但这并不能解释你的例外情况。你用
但决不要设置为对象的实例。然后,尝试检查ojbect的这个实例是否为null
您需要确保在创建的属性中设置了一些值。在按钮单击事件上也执行此操作
if (thePickups != null)
{
}
出于好奇,代码块底部的浮动周期是多少?您在哪里得到异常?您在哪里设置约会?在单击按钮之前,您需要确保pickups为非null,但我认为您在任何地方都没有这样做……您没有提到发生错误的行。只是一个建议:在VisualStudio中,转到Debug/Exceptions菜单,然后检查公共语言运行时异常:抛出和未处理。然后,当在“调试”中运行时,当抛出异常时,您的应用程序将停止,您将看到该位置。很抱歉,没有指定,这是我的按钮单击后的第一行,即pickups.custName=textBox1.Text;很抱歉,我有点困惑,我想当我定义Pickups来创建object的实例时,你只定义类,而不是创建object的实例。private Pickups Pickups只定义Pickups类型的变量。要创建类Pickups的实例,必须使用Pickups=new Pickups;
private Pickups thePickups;
//The pickups object being created/edited
public Pickups appointment
{ //Property to allow access to thePickups
get { return thePickups; }
set { thePickups = value; }
}
if (thePickups != null)
{
}
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
}
}