VB.NET - nullreferenceexception was unhandled - Asked By rahul krishna on 23-Jun-13 07:48 PM

 Private Sub create_mouse()
        'Dim mouse As New PictureBox
        Dim mouse As New PictureBox
        With mouse
            .Width = 10
            .Height = 10
            .BackColor = Color.Red
            .Top = r.Next(PictureBox1.Top, PictureBox1.Bottom - 10)     //error null reference exception was handeled//
            .Left = r.Next(PictureBox1.Left, PictureBox1.Right - 10)
        End With
        Me.Controls.Add(mouse)
        mouse.BringToFront()
Robbe Morris replied to rahul krishna on 23-Jun-13 07:47 PM
either PictureBox1 is null or the attempt to move the mouse to an invalid position is what is firing the exception.
rahul krishna replied to Robbe Morris on 24-Jun-13 12:55 AM
how to solve this error ?can u help me pls
Robbe Morris replied to rahul krishna on 24-Jun-13 05:10 AM
run this in debug mode and mouse over the variable objects and variables to see which one is either null or clearly has invalid values.
rahul krishna replied to Robbe Morris on 24-Jun-13 12:04 PM

the following is my full program code for create a snake game ,i couldn't find the error  in this can u check this one
Public Class Form2
#Region "snake stuff"
    Dim snake(1000) As PictureBox
    Dim length As Integer = -1
    Dim left_right_mover As Integer = 0
    Dim up_down_mover As Integer = 0
    Dim r As Random

    Private Sub create_head()
        length += 1
        snake(length) = New PictureBox
        With snake(length)
            .Height = 10
            .Width = 10
            .BackColor = Color.White
            .Top = (PictureBox1.Top + PictureBox1.Bottom) / 2
            .Left = (PictureBox1.Left + PictureBox1.Right) / 2
        End With
        Me.Controls.Add(snake(length))
        snake(length).BringToFront()

        lengthensnake()
        lengthensnake()

    End Sub
    Private Sub lengthensnake()
        length += 1
        snake(length) = New PictureBox
        With snake(length)
            .Height = 10
            .Width = 10
            .BackColor = Color.White
            .Top = snake(length - 1).Top
            .Left = snake(length - 1).Left + 10
        End With
        Me.Controls.Add(snake(length))
        snake(length).BringToFront()
    End Sub
    Private Sub Form2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        Select Case e.KeyChar
            Case "a"
                left_right_mover = -10
                up_down_mover = 0
            Case "d"
                left_right_mover = 10
                up_down_mover = 0
            Case "e"
                up_down_mover = -10
                left_right_mover = 0
            Case "s"
                up_down_mover = 10
                left_right_mover = 0
        End Select
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        For i = length To 1 Step -1
            snake(i).Top = snake(i - 1).Top
            snake(i).Left = snake(i - 1).Left
        Next

        snake(0).Top += up_down_mover
        snake(0).Left += left_right_mover


        collide_with_mouse()

    End Sub

#End Region

#Region "mouse stuff"

    Dim mouse As New PictureBox

    Private Sub create_mouse()
        mouse = New PictureBox

        With mouse
            .Width = 10
            .Height = 10
            .BackColor = Color.Red
            .Top = r.Next(PictureBox1.Top, PictureBox1.Bottom - 10) //null exception unhandeled //

            .Left = r.Next(PictureBox1.Left, PictureBox1.Right - 10)
        End With
        Me.Controls.Add(mouse)
        mouse.BringToFront()

    End Sub
#End Region
#Region "collission"
    Private Sub collide_with_mouse()
        Dim mouse As New PictureBox
        If snake(0).Bounds.IntersectsWith(mouse.Bounds) Then
            lengthensnake()
            mouse.Top = r.Next(PictureBox1.Top, PictureBox1.Bottom - 10)
            mouse.Left = r.Next(PictureBox1.Left, PictureBox1.Right - 10)
        End If
    End Sub
#End Region
   
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        create_head()
        Timer1.Start()
        create_mouse()

    End Sub

    
End Class
rahul krishna replied to Robbe Morris on 30-Jun-13 12:54 AM
heelo sir pls check my last message ,i have send my code pls find the error and correct it