How to solve "Object reference not set to an instance of an object?

Object reference not set to an instance of an object.?

  • I tried to show my Form2 by calling Form2.Show() but it says Object reference not set to an instance of an object. What do I do? Form2 code below: Public Class Form2 Dim location_rock As Integer = RectangleShape1.Location.Y Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long) Private Sub Form2_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.J Then PictureBox1.Location = New Point(75, PictureBox1.Location.Y) End If End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Button1.Visible = False Call rockmove() End Sub Private Sub rockmove() Do Until RectangleShape1.Location.Y = 529 location_rock = location_rock + 2 RectangleShape1.Location = New Point(RectangleShape1.Location.X, location_rock) Sleep(300) Loop End Sub End Class

  • Answer:

    statements like this could be the cause: location_rock = location_rock + 2 if location_rock had a null value if it's happening when calling .show() it could even be that the Form2 object is null and hasn't been instantiated. Try to debug and look for variables that are null

Interesting Articles at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.