Creating an Array
Creating an Array
Creating an Array
Creating an Array
Creating an Array
Creating an Array Creating an Array Creating an Array Creating an Array Creating an Array Creating an Array Creating an Array Creating an Array
Creating an Array Creating an Array
Creating an Array
Go Back  Xtreme Visual Basic Talk > > > Creating an Array


Reply
 
Thread Tools Display Modes
  #1  
Old 05-20-2011, 06:22 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Question Creating an Array


Hi, I've started to reprogram my if / end if statements into an array to simply shorten the code. I have tried making my own, without success. I have declared the array at the top of my class. I then made a sub to assign each pciture box an array location. I then made a For next loop which contained the array and a nested IF statement. It didn't work. I get a "System.NullReferenceException – Object reference not set to an instance of an object". runtime error on the first line of my loop.

Code:

Code:
    Public BodyArray(52) As PictureBox

_________________________________

    Sub BodyArraySub()

        BodyArray(0) = imgsnake
        BodyArray(1) = imgsnake2
        BodyArray(2) = imgsnake3
        BodyArray(3) = imgsnake4
        BodyArray(4) = imgsnake5
        BodyArray(5) = imgsnake6
        BodyArray(6) = imgsnake7
        BodyArray(7) = imgsnake8
        BodyArray(8) = imgsnake9
        BodyArray(9) = imgsnake10

        BodyArray(10) = imgsnake11
        BodyArray(11) = imgsnake12
        BodyArray(12) = imgsnake13
        BodyArray(13) = imgsnake14
        BodyArray(14) = imgsnake15
        BodyArray(15) = imgsnake16
        BodyArray(16) = imgsnake17
        BodyArray(17) = imgsnake18
        BodyArray(18) = imgsnake19
        BodyArray(19) = imgsnake20

        BodyArray(20) = imgsnake21
        BodyArray(21) = imgsnake22
        BodyArray(22) = imgsnake23
        BodyArray(23) = imgsnake24
        BodyArray(24) = imgsnake25
        BodyArray(25) = imgsnake26
        BodyArray(26) = imgsnake27
        BodyArray(27) = imgsnake28
        BodyArray(28) = imgsnake29
        BodyArray(29) = imgsnake30


        BodyArray(30) = imgsnake31
        BodyArray(31) = imgsnake32
        BodyArray(32) = imgsnake33
        BodyArray(33) = imgsnake34
        BodyArray(34) = imgsnake35
        BodyArray(35) = imgsnake36
        BodyArray(36) = imgsnake37
        BodyArray(37) = imgsnake38
        BodyArray(38) = imgsnake39
        BodyArray(39) = imgsnake40

        BodyArray(40) = imgsnake41
        BodyArray(41) = imgsnake42
        BodyArray(42) = imgsnake43
        BodyArray(43) = imgsnake44
        BodyArray(44) = imgsnake45
        BodyArray(45) = imgsnake46
        BodyArray(46) = imgsnake47
        BodyArray(47) = imgsnake48
        BodyArray(48) = imgsnake49
        BodyArray(49) = imgsnake50

        BodyArray(50) = imgsnake51
        BodyArray(51) = imgsnake52

    End Sub


' my nested If loop with array integration:

   ' Snake body collision

        For Each bodyArrayItem In BodyArray

            If BodyArray(0).Visible = True Then ' currently crashes here
                If imgsnake.Bounds.IntersectsWith(BodyArray(2).Bounds) Then
                    P1movement.Enabled = False
                    SnakeX_SinglePlayer_Dialog.Show()
                    prevscore_time.Enabled = True
                    basic_score.Enabled = False
                    buttontimer.Enabled = True
                End If
            End If
        Next bodyArrayItem
what am I doing wrong? sorry for the ignorance, I am a learning beginner. thanks in advance.
Reply With Quote
  #2  
Old 05-20-2011, 07:31 AM
PlausiblyDamp's Avatar
PlausiblyDampCreating an Array PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

When you declare your array as
Code:
Public BodyArray(52) As PictureBox
you are declaring an array that can hold 53 picturebox controls - all of the elements will be set to Nothing though. Attempting to access the visible property of a null element will create the error you are getting.

If you put a breakpoint on the line that fails what is the content of BodyArray(0)? Are you sure you are calling the code that initialises the array before you are trying to access the array?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 05-20-2011, 08:53 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Quote:
Originally Posted by 45minutes View Post

Code:
' my nested If loop with array integration:

   ' Snake body collision

        For Each bodyArrayItem In BodyArray

            If BodyArray(0).Visible = True Then ' currently crashes here
                If imgsnake.Bounds.IntersectsWith(BodyArray(2).Bounds) Then
                    P1movement.Enabled = False
                    SnakeX_SinglePlayer_Dialog.Show()
                    prevscore_time.Enabled = True
                    basic_score.Enabled = False
                    buttontimer.Enabled = True
                End If
            End If
        Next bodyArrayItem
Is it crashing here on the very first loop through??? "If BodyArray(0).Visible = True Then ' currently crashes here"

put your break point PlausiblyDamp mentioned here "For Each bodyArrayItem In BodyArray"

before it enters the For loop and the step through. I see you are not incrementing these values "BodyArray(0).Visible" if it crashes a few loops in it could be related to the position of the array index.

Edit: or the second line here "If BodyArray(0).Visible = True Then ' currently crashes here" as it will still stop there first time through.
Reply With Quote
  #4  
Old 05-20-2011, 11:09 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

ohk i'll start on more simpler function. On form_load event i set every picturebox to invisible except the first one.

my code:

Code:
  For Each bodyArrayItem In BodyArray

            BodyArray(1).Visible = False ' currently crashes here
         
        Next bodyArrayItem
error states "Object reference not set to an instance of an object."
"NullreferenceException was unhanded"

do I have to do something to the array first? I already have 52 pictureboxes on my form, and just want them to be assigned to an index in an Array.

Last edited by 45minutes; 05-21-2011 at 05:19 AM.
Reply With Quote
  #5  
Old 05-21-2011, 06:46 AM
masterpreenx masterpreenx is offline
Newcomer
 
Join Date: Sep 2010
Location: Davao City Philippines
Posts: 24
Default

for ech bodyArrayItem in BodyArray
BodyArray(1).Visible = False
next body Array Item

what you lack is

for ech bodyArrayItem in BodyArray
BodyArray(1) = New Picturebox ' you need this on every declaration of a picturebox/label/etc.
BodyArray(1).Visible = False
next body Array Item

This will do the trick
Reply With Quote
  #6  
Old 05-21-2011, 08:53 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Hi, I have found the solution. As it turns out i was speaking to a friend, and I realised I didn't have the BodyArraySub() in the form_Load event Which is ESSENTIAL, so that the picture boxes are assigned with an array index. that 1 line of code disrupted everything. Thanks once again for keeping me motivated, this community has great support. that last post I'm still working on.

EDIT: and once again, this problem is fixed thanks

Last edited by 45minutes; 05-21-2011 at 09:22 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Creating an Array
Creating an Array
Creating an Array Creating an Array
Creating an Array
Creating an Array
Creating an Array Creating an Array Creating an Array Creating an Array Creating an Array Creating an Array Creating an Array
Creating an Array
Creating an Array
 
Creating an Array
Creating an Array
 
-->