Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried Timer control in Dynamic but it throws an error as "An unhandled exception of type 'System.NullReferenceException' occurred in ConsoleApplicationworking.exe"
"Additional information: Object reference not set to an instance of an object"

C#
private Timer Tims;
protected void SetControl()
       {
         new Timer();
         Tims.Tick += new EventHandler(timer1_Tick);
       }

private void StartBtn_Click(object sender, EventArgs e)
       {

           if (Stimer != 0)
           {
               Tims.Interval = Stimer;
               Tims.Enabled = true;
               Tims.Start();
           }
           else
           {
               MessageBox.Show("Select TimerInterval");
           }

       }



Kindly solve this problem.

What I have tried:

C#
int Stimer=1000;(From User)
 private Timer Tims;
 protected void SetControl()
        {
          new Timer();
          Tims.Tick += new EventHandler(timer1_Tick);
        }

 private void StartBtn_Click(object sender, EventArgs e)
        {

            if (Stimer != 0)
            {                
                Tims.Interval = Stimer;
                Tims.Enabled = true;
                Tims.Start();
            }
            else
            {
                MessageBox.Show("Select TimerInterval");
            }

        }
Posted
Updated 22-Aug-16 7:44am
Comments
[no name] 22-Aug-16 8:15am    
Tims= new Timer(); instead of simply new Timer();.
ZurdoDev 22-Aug-16 8:19am    
I suggest you post as solution.
[no name] 22-Aug-16 8:25am    
Done, thanks.
ZurdoDev 22-Aug-16 8:20am    
The previous comment is the answer to your question, but for future reference, if you get an error, it tells you exactly what is happening. And if you pay attention to what line of code is causing the error you would soon see that you are trying to reference something that is null, which is a very, very easy problem to fix on your own.
vinodh muthusamy 22-Aug-16 8:20am    
Thanks a lot working fine

1 solution

This is probably just a clerical error.

Your code is:
C#
new Timer();

instead of:
C#
Tims= new Timer(); 

Please read also the comment by RyanDev.
 
Share this answer
 
Comments
CPallini 22-Aug-16 8:29am    
5.
[no name] 22-Aug-16 8:30am    
Thank you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900