473,289 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

object ref not set error

12
hi i am new to timer. This is me code. can anyone help me?


[code]
class Class1
{
static Timer myTimer;
public Class1()
{
myTimer = new Timer();
}
public static void Main()
{
myTimer.Elapsed += new ElapsedEventHandler( add );
myTimer.Interval = 1000;
myTimer.Start();

while ( Console.Read() != 'q' )
{ ;
}
}
public static void add( object source, ElapsedEventArgs e3 )

{
if(source==myTimer)
{
Console.WriteLine("Sample");
Console.Write("\r{0}", DateTime.Now);
Console.Write("\t"+(3+4));
}
}
}
Nov 22 '07 #1
1 804
r035198x
13,262 8TB
hi i am new to timer. This is me code. can anyone help me?


[code]
class Class1
{
static Timer myTimer;
public Class1()
{
myTimer = new Timer();
}
public static void Main()
{
myTimer.Elapsed += new ElapsedEventHandler( add );
myTimer.Interval = 1000;
myTimer.Start();

while ( Console.Read() != 'q' )
{ ;
}
}
public static void add( object source, ElapsedEventArgs e3 )

{
if(source==myTimer)
{
Console.WriteLine("Sample");
Console.Write("\r{0}", DateTime.Now);
Console.Write("\t"+(3+4));
}
}
}
The Main method is the entry point of your program. That is where the statements start getting executed from. Now look at your code again and you will see that myTimer is still null when the first statement is executed, yet you are trying to dereference it.
Note that the new operator is used for creating new objects.

Timer myTimer;

simply creates a new variable not a new object.
The variable myTimer is still refering to null (no object) after that statement is executed.
Nov 22 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
2
by: Nithi Gurusamy | last post by:
Dear Group: I have a COM object developed in VB. It makes ADODB calls. When it fails it Raise Error. I am using the COM object in my ASP using Server.CreateObject. Whenever a function call fails...
9
by: Keith Rowe | last post by:
Hello, I am trying to reference a Shockwave Flash Object on a vb code behind page in an ASP.NET project and I receive the following error: Guid should contain 32 digits with 4 dashes...
0
by: Dirk Försterling | last post by:
Hi all, a few days ago, I upgraded from PostgreSQL 7.2.1 to 7.4, following the instructions in the INSTALL file, including dump and restore. All this worked fine without any error (message). ...
1
by: Nithi Gurusamy | last post by:
Dear Group: I have a COM object developed in VB. It makes ADODB calls. When it fails it Raise Error. I am using the COM object in my ASP using Server.CreateObject. Whenever a function call fails...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.