C# 对象引用未设置为对象日期/时间的实例
为什么我总是犯这个错误?“对象引用未设置为对象的实例。” 以下是代码和错误指向的位置:C# 对象引用未设置为对象日期/时间的实例,c#,winforms,C#,Winforms,为什么我总是犯这个错误?“对象引用未设置为对象的实例。” 以下是代码和错误指向的位置: namespace Sell_System { public partial class Form4 : Form { private TextBox dateContainer; private TextBox timeContainer; private Timer timer; public Form4() { InitializeComponent();
namespace Sell_System
{
public partial class Form4 : Form
{
private TextBox dateContainer;
private TextBox timeContainer;
private Timer timer;
public Form4()
{
InitializeComponent();
dateContainer.Text = DateTime.Now.ToString("dddd, MMMM dd, yyyy", System.Globalization.CultureInfo.InvariantCulture);
timeContainer.Text = DateTime.Now.ToString("h:mm tt", System.Globalization.CultureInfo.InvariantCulture);
timer.Tick += new System.EventHandler(this.StartTimer);
timer.Interval = 60000;
timer.Start();
}
我之所以将其放入public Form4()中,是因为我希望时间总是每60秒更新一次,当时间达到00:00AM时,日期将增加一天
错误指向dateContainer.Text,当我对该命令进行注释时,错误将指向timeContainer.Text,依此类推,直到timer.Start() 当您将类实例设置为
private TextBox dateContainer;
在程序的某个部分给它赋值之前,它将为空。您需要在讲师或init函数中编写
dateContainer=…
。当您创建类实例时,如
private TextBox dateContainer;
在程序的某个部分给它赋值之前,它将为空。您需要在讲师或init函数中编写
dateContainer=…
。在winform应用程序上声明和初始化控件的工作在您将控件放在窗体表面时完成。通过这种方式,WinForm设计器将适当的代码添加到InitializeComponent中,您可以在代码中使用控件
如果您手动添加控件(如您所做的),则您有责任进行初始化,然后定义其一些基本属性并将这些控件添加到表单控件集合
像这样的
namespace Sell_System
{
public partial class Form4 : Form
{
// Declaration of your controls....
private TextBox dateContainer;
private TextBox timeContainer;
private Timer timer;
public Form4()
{
// This is were the controls defined by the form designer will be initialized
// using all the default values for their property
InitializeComponent();
// Now you do it manually for the controls added manually
dateContainer = new TextBox();
// At least define the position where the control should appear on the form surface
dateContainer.Location = new Point(10, 10);
dateContainer.Text = DateTime.Now.ToString("dddd, MMMM dd, yyyy", System.Globalization.CultureInfo.InvariantCulture);
timeContainer = new TextBox();
timeContainer.Location = new Point(30, 10);
timeContainer.Text = DateTime.Now.ToString("h:mm tt", System.Globalization.CultureInfo.InvariantCulture);
// To be shown the controls should be added to the Form controls collection
this.Controls.Add(dateContainer);
this.Controls.Add(nameContainer);
// The WinForm timer is just a component so it is enough to Initialize it
timer = new System.Windows.Forms.Timer();
timer.Tick += new System.EventHandler(this.StartTimer);
timer.Interval = 60000;
timer.Start();
}
}
如果必须定义控件的许多属性,以这种方式创建控件可能会很快变得混乱。因此,如果动态需求确实不需要,手动创建控件并不是一个好的做法。在winform应用程序上声明和初始化控件的工作是在将控件放在窗体表面时完成的。通过这种方式,WinForm设计器将适当的代码添加到InitializeComponent中,您可以在代码中使用控件 如果您手动添加控件(如您所做的),则您有责任进行初始化,然后定义其一些基本属性并将这些控件添加到表单控件集合 像这样的
namespace Sell_System
{
public partial class Form4 : Form
{
// Declaration of your controls....
private TextBox dateContainer;
private TextBox timeContainer;
private Timer timer;
public Form4()
{
// This is were the controls defined by the form designer will be initialized
// using all the default values for their property
InitializeComponent();
// Now you do it manually for the controls added manually
dateContainer = new TextBox();
// At least define the position where the control should appear on the form surface
dateContainer.Location = new Point(10, 10);
dateContainer.Text = DateTime.Now.ToString("dddd, MMMM dd, yyyy", System.Globalization.CultureInfo.InvariantCulture);
timeContainer = new TextBox();
timeContainer.Location = new Point(30, 10);
timeContainer.Text = DateTime.Now.ToString("h:mm tt", System.Globalization.CultureInfo.InvariantCulture);
// To be shown the controls should be added to the Form controls collection
this.Controls.Add(dateContainer);
this.Controls.Add(nameContainer);
// The WinForm timer is just a component so it is enough to Initialize it
timer = new System.Windows.Forms.Timer();
timer.Tick += new System.EventHandler(this.StartTimer);
timer.Interval = 60000;
timer.Start();
}
}
如果必须定义控件的许多属性,以这种方式创建控件可能会很快变得混乱。因此,如果动态需求确实不需要,手动创建控件并不是一个好的做法。这些控件实例是在InitializeComponent调用中初始化的吗?这些实例是在
公共分部类(您的表单名称):(表单)
可能错误在InitializeComponent()中
。如果可能的话,也发布代码。另外,请澄清您使用的是哪种版本的c#。类Fomr4的名称是否与定义这些控件的设计器类相同。只需在InitializeComponent后的第一行上放置断点即可。运行代码,当它到达断点时,告诉我们dateContainer
是否为空。如果为null,则您尚未初始化。这些控件实例是在InitializeComponent调用中初始化的吗?这些实例是在公共分部类(您的表单名称):(表单)
之后初始化的,可能错误在InitializeComponent()
中。如果可能的话,也发布代码。另外,请澄清您使用的是哪种版本的c#。类Fomr4的名称是否与定义这些控件的设计器类相同。只需在InitializeComponent后的第一行上放置断点即可。运行代码,当它到达断点时,告诉我们dateContainer
是否为空。如果它是空的,那么你还没有初始化它,好的。我刚刚意识到我在表单加载
中初始化了它们,而不是在初始化组件
之后的表单4
中,这就是程序无法识别它们的原因。哈哈。顺便说一下,谢谢史蒂夫先生。如果你不介意的话,我想问一个问题。有没有办法使程序中的时间与系统(windows)中的时间完全相同?所以,就像我在(下午6:50:25)运行程序一样。哦,我现在才看到你的评论。可能您已经自己解决了,但只需更改DateTime.Now.ToString(“h:mm:ss tt”,System.Globalization.CultureInfo.InvariantCulture)
;哦,好的。我刚刚意识到我在表单加载
中初始化了它们,而不是在初始化组件
之后的表单4
中,这就是程序无法识别它们的原因。哈哈。顺便说一下,谢谢史蒂夫先生。如果你不介意的话,我想问一个问题。有没有办法使程序中的时间与系统(windows)中的时间完全相同?所以,就像我在(下午6:50:25)运行程序一样。哦,我现在才看到你的评论。可能您已经自己解决了,但只需更改DateTime.Now.ToString(“h:mm:ss tt”,System.Globalization.CultureInfo.InvariantCulture)
;