Object reference not set to an instance of an object

Simon Scott 306 Reputation points
2021-04-21T15:23:50.637+00:00

Good afternoon,

I have created a little application in VB.net which runs fine on my Windows 10 PC but when i run it on another one i get the following message...

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at Timezone_Client.Form1.Timer1_Tick(Object sender, EventArgs e)
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

This is the first part of the code in my application which i believe it relates to.

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    Timer1.Interval = 10000
    timer1.Start()

Can anyone assist me please?

I am a novice, so please bear with me!

Thanks
Simon

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,579 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Simon Scott 306 Reputation points
    2021-04-21T16:04:27.947+00:00

    Thanks for your reply.

    I simply dragged a timer onto the form and then put that code under the Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick event.

    89987-screenshot-2021-04-21-170327.png

    Apologies if that's not what you meant!

    Thanks
    Simon


  2. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-04-22T05:47:03.973+00:00

    Hi @Simon Scott ,
    Try starting the timer outside the Tick event:

        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick  
            ' do something here.  
        End Sub  
      
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            Timer1.Interval = 10000  
            Timer1.Start()  
        End Sub  
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
            Timer1.Stop()  
        End Sub  
    

    Click Button1 to start the timer and Button2 to stop it.
    Besides, set ‘Enabled’ property 'false'.
    90177-image.png
    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Simon Scott 306 Reputation points
    2021-04-22T13:02:50.827+00:00

    Ok, so i think i may have found where the issue is...

    90392-image.png

    I do have the line Dim sString As String = Nothing further up the page within the same sub

    Any advice?

    Many thanks
    Simon

    0 comments No comments

  4. Simon Scott 306 Reputation points
    2021-04-22T15:11:23.703+00:00

    I believe i have fixed this so hold fire on any possible answers :)

    0 comments No comments