to all,

I'm a newbie to vb.net, currently I'm using vb.net ver. 2003, I'm trying to add a clock to my project. The following is what I have:

Dim timer1 As Timer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
timer1.Interval = 10000
Timer1.Start()
End Sub
Private Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
lblTimer1.Text = System.DateTime.Now

End Sub


End Class

when I try to comply this program I'm getting errors, which are: "An unhandled exception of type 'System.NullReferenceException' occurred in clk_test.exe"

Then there is a yellow arrow point to timer1.Interval = 1000.

Can someone pls. help......
thanks
mixthis

Dani AI

Generated

The exception means the Timer reference is Nothing when you try to use it. That happens when a Timer variable is declared but never instantiated, or when a designer-created Timer is accidentally hidden by another declaration with the same name. As suggested, using the Toolbox Timer is the simplest fix; as noted, if you declare a Timer yourself you must create an instance; and 's point about Enabled is correct — Start simply sets Enabled = True.

Two practical fixes:

  • Use the Windows Forms Timer from the Toolbox: the designer will create and initialize the component for you, you can set Interval (milliseconds) in Properties, and double-click the timer icon to generate a Tick handler that runs on the UI thread.
  • If you prefer code-only, create the Timer instance and attach the Tick handler so the runtime actually has an object to call. Here is a minimal example (different names than the thread to avoid shadowing):
Dim tmr As New System.Windows.Forms.Timer()
AddHandler tmr.Tick, AddressOf OnTick
tmr.Interval = 1000
tmr.Start()

Private Sub OnTick(sender As Object, e As EventArgs)
    lblTimer1.Text = DateTime.Now.ToString("HH:mm:ss")
End Sub

Troubleshooting tips: set a breakpoint in Form1_Load and inspect the timer variable (is it Nothing?), look for duplicate declarations named Timer1 (designer + manual), and remember that System.Windows.Forms.Timer runs on the UI thread (use other Timer types only when appropriate). Reference material: NullReferenceException docs, System.Windows.Forms.Timer docs, AddHandler statement.

Recommended Answers

All 4 Replies

Try to use the Timer object from the toolbox instead of declaring your own Timer object. If you declare a Timer object you should also create an instante of that object. Your code does not make an instante of the Timer object.

I have tested your code on the form load event and when the timer tick event and it works OK but not able to simulate your problem. Probably you may just you
my suggestion on top


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
timer1.Interval = 10000
Timer1.Start()
End Sub

As far as I know, you also need to use "Timer1.Enabled = True" to start the timer, and "Timer1.Enabled = False" to turn the timer off. You also probably want a timer interval of 1000 to update every second as opposed to every 10 seconds, this is easier to set up in the object properties as opposed to in a Form1_Load event.

Hi,

Dim timer1 As Timer = new Timer

This will cure the null reference.

Loren Soth

did the timer enabled = true work? sounds like that was the issue to me..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.