0

I'm creating a simple game as a Windows Form Application and am trying to program collision detection between the player's picture box, and walls within the game, which are also picture boxes. Instead of having to program every wall with an IF statement I decided to use a For loop.

I first put the picture boxes into an array:

Dim walls() As PictureBox = {wall1, wall2, wall3, wall4, wall5, wall6, wall7, wall8, wall9, wall10, wall11, wall12, wall13, wall14, wall15}

Which I then used to loop through as the collision detection (picCar is the player):

        For x As Integer = 0 To 14 'I also tried this with "1 To 1"
        If picCar.Bounds.IntersectsWith(walls(x).Bounds) Then
            If lives > 1 Then
                lives -= 1
                movetostart()
            Else
                MsgBox("Game Over!")
            End If
        End If
    Next

This returned a "NullReferenceException was unhandled" error so I tried it within a For Each loop instead:

        For Each wall As PictureBox In walls
        If picCar.Bounds.IntersectsWith(wall.Bounds) Then
            If lives > 1 Then
                lives -= 1
                movetostart()
            Else
                MsgBox("Game Over!")
            End If
        End If
    Next

This returned the same error. I'm not quite sure what I'm doing wrong? I found and tested both these ideas through other threads and cannot find much else on the matter, would you be able to help?

Links to threads:

Make Images intersect and gravity in VB

VB.NET collision between pictureboxes

Community
  • 1
  • 1
RyanHx
  • 411
  • 3
  • 10
  • What line generates the Exception? – Sam Axe Mar 16 '15 at 19:31
  • 2
    This is where you use the debugger and follow code to step into each line and find which object was not initialized. – OneFineDay Mar 16 '15 at 19:35
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – The Blue Dog Mar 16 '15 at 19:42
  • almost certainly your walls array contains 4 Nothings. See [Null Reference in VB](http://stackoverflow.com/a/26761773/10704520) for details but you need to initialize the array contents *after* the pictureboxes are created (like in Form Load) – Ňɏssa Pøngjǣrdenlarp Mar 16 '15 at 19:52
  • @Dan-o In both instances it is the first IF statement that produces the error. – RyanHx Mar 16 '15 at 20:26
  • @plutonix I don't quite understand how it is a null value when the array has the wall picture boxes declared within it? I have already made the picture boxes within the form so it is referring to something? – RyanHx Mar 16 '15 at 20:27
  • **But** as the linked answer explains, if you are creating the array before the form is created, none of the controls can exist yet, thus `wall1` et al are Nothing – Ňɏssa Pøngjǣrdenlarp Mar 16 '15 at 20:36
  • @Plutonix I understand what you mean, but I'm not sure how to implement it? Forgive me, I'm relatively new to programming. – RyanHx Mar 16 '15 at 21:08
  • This is covered in the [VB NRE Answer](http://stackoverflow.com/a/26761773/1070452) which has giant headers for each type of problem. Scroll to the giant, bold easy to find **Visual Basic Forms** header which describes this very thing – Ňɏssa Pøngjǣrdenlarp Mar 16 '15 at 21:14

0 Answers0