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

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.