Reputation: 25
Why did i keep getting this error? "Object reference not set to an instance of an object."
Here is the code and where is the error pointing at:
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();
}
The reason why i put it into public Form4() was because i want time always updating every 60 seconds and when the time is reached 00:00AM, the date will increased by one day.
The error pointed at dateContainer.Text and when i comment that command, the error will be pointed at timeContainer.Text and so on until timer.Start();
Upvotes: 1
Views: 2766
Reputation: 216333
The work of declaring and initializing controls on a winform application is done when you put your controls on the surface of your form. In that way the WinForm designer add the appropriate code to the InitializeComponent and you could use your controls in your code.
If you add your control manually, as you have done, then it is your responsability to initialize then, define some of their basic properties and add these controls to the form control collection.
Something like this
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();
}
}
Creating controls in this way could become messy very fast if you have to define many properties of the controls. So,if not really needed by a dynamic requirement, a manual creation of the controls is not really a good practice.
Upvotes: 1
Reputation: 6723
When you make a class instance like
private TextBox dateContainer;
It will be null until some part of the program assigns a value to it. You need to write dateContainer = ...
in the instructor or the init function.
Upvotes: 0