Question

I am using the following code but getting error

Object reference not set to an instance of an object" on line 2.

System.Timers.Timer[] timer = new System.Timers.Timer[10];
timer[0].Elapsed += new ElapsedEventHandler(DoJob);

Why?

Was it helpful?

Solution

you forget to initialize array's item

System.Timers.Timer[] timer = new System.Timers.Timer[10];
timer[0] = new System.Timers.Timer();
timer[0].Elapsed += new ElapsedEventHandler(DoJob);

OTHER TIPS

Your first line creates an array. An array of Timer references. An array of Timer references that all have a value of null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top