question

SimonScott-1980 avatar image
0 Votes"
SimonScott-1980 asked SimonScott-1980 answered

Object reference not set to an instance of an object

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

dotnet-visual-basic
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


It seems that Timer1 variable is Nothing. Show some details about Timer1. How did you create the timer that generates the Timer1_Tick event?


0 Votes 0 ·
SimonScott-1980 avatar image
0 Votes"
SimonScott-1980 answered Viorel-1 commented

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


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Did you determine the line where the exception was detected? Usually, Visual Studio indicates that line. If the exception is in other parts of Timer1_Tick, then show more code details.


0 Votes 0 ·
XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered XingyuZhao-MSFT edited

Hi @SimonScott-1980 ,
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.



image.png (1.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimonScott-1980 avatar image
0 Votes"
SimonScott-1980 answered

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


image.png (52.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimonScott-1980 avatar image
0 Votes"
SimonScott-1980 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.