ADVANCED: Arraylist "accidentally" refers to null objects

bloukewer

Well-known member
Joined
Jul 2, 2004
Messages
88
Programming Experience
3-5
I have a button which executes the following (pseudo) code when clicked:

dim tmp as new employee
tmp.Name = txtName.text
tmp.Surname = txtSurname.text
Arraylist1.add(tmp)
...
'do something that calls each element of Arraylist1

Now, the code executes perfectly the first time. The second time I push the button though, it throws a null-reference exception. Upon evaluation it is evident that the arraylist did not add a new object to the arraylist, but instead just modified tmp and moved it to the next position in the arraylist, leaving the preceding arraylist pointer empty.

Thus, after the second click, arraylist(0) is empty and arraylist(1) contains the new object.

Can someone please tell me how I can overcome this problem.

I do know of one solution, which is to create a constructor for the element-type of the arraylist, which takes as parameters all the values of all the fields and initializes the element's values. However, the class structures is often big, thus this would be impractical.

I have less than 2 days to do a few thousand lines of programming, and I have been racking my brain on this for more than an hour! Any help would be much appreciated.
 
What you just described is not possible. Calling Add on an Arraylist cannot possibly "move" an item. I suggest you check the contents of the ArrayList before this code gets executed and see whether the existing item is already a null reference. If not, it must be getting set to Nothing somewhere else. The code you have posted will do exactly what you want it to, so you must have omitted something.
 
Nevermind, just one frikkin line (out of a few thousand) that I forgot.

But I guess that's what happens when you work for 2.5 days without any sleep.
 
Back
Top