0

I have this code which is used for countdown in C#. I can't seem to find why my variable t is null. I tried this code on a separate project and it works well. I tried to incorporate it into another project and it says that variable t is null.

public partial class tracker : Form
{
    System.Timers.Timer t;
    int h1, m1, s1;

    public tracker()
    {
        InitializeComponent();
    }

    private void tracker_Load(object sender, EventArgs e)
    {
        t = new System.Timers.Timer();
        t.Interval = 1000; //1s
        t.Elapsed += OnTimeEventWork;
    }

    private void btnLogin_Click(object sender, EventArgs e)
    {
        t.Start();
        btnLogin.Enabled = false;
        richTextBox1.SelectionLength = 0;
        richTextBox1.SelectedText = DateTime.Now.ToString("MM/dd/yyyy\n");
        richTextBox2.SelectedText = "Time In\n";
        richTextBox3.SelectedText = DateTime.Now.ToString("HH:mm:ss\n");
        richTextBox4.SelectedText = "\n";
        richTextBox5.SelectedText = "\n";
    }
}
fatihyildizhan
  • 7,103
  • 5
  • 54
  • 74
  • the system recognizes the 't.Start();' null. thanks –  Apr 28 '21 at 09:39
  • tracker_load is not being called before btnLogin_Click. Check you set an event handler or move to an 'OnLoad' method, or move to the ctor, or else to id at the start of the click handlier. – mikelegg Apr 28 '21 at 09:41
  • Any specific reason for creating an instance of `Timer` class inside `tracker_Load` event? – Prasad Telkikar Apr 28 '21 at 09:42
  • Looks like `tracker_Load` is attached to the Load event of the form... are you able to debug the code of `tracker_Load`? – Chetan Ranpariya Apr 28 '21 at 09:43
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Self Apr 28 '21 at 09:45
  • Thank you everyone for the help, Mr. Prasad already gave the perfect solution. Thank you so much! –  Apr 28 '21 at 09:50

1 Answers1

2

You are getting error t as null, because t.Start() is calling before instantiation of Timer object t.

To solve this issue, either instantiate before t.start() or create an object inside the constructor.

Like

public tracker()
{
     InitializeComponent();

     //Here you can instantiate Timer class
     t = new System.Timers.Timer();
     t.Interval = 1000; //1s
     t.Elapsed += OnTimeEventWork;
}

private void tracker_Load(object sender, EventArgs e)
{
    //Do NOT create object of Timer class here
}

private void btnLogin_Click(object sender, EventArgs e)
{
         
   t.Start();
   ...
}
Prasad Telkikar
  • 10,243
  • 2
  • 12
  • 31
  • I have no clue why you are using `tracker_load` event so I can;t answer this question, but if this event is only instantiating `Timer` class then yes you can remove it – Prasad Telkikar Apr 28 '21 at 09:48
  • Yes I only used it instantiate `Timer` class. I tried the code you've replied an it worked. Thank you so much for the help! –  Apr 28 '21 at 09:49
  • If that is the case then remove `tracker_load` function and I am glad my solution worked for you. – Prasad Telkikar Apr 28 '21 at 09:50