ArrayList

  • Thread starter Thread starter rn5a
  • Start date Start date
R

rn5a

What's wrong with the following code?

Dim arrColors(5) As ArrayList

arrColors(5) = New ArrayList
arrColors(0).Add("Red")
arrColors(1).Add("Blue")
arrColors(2).Add("Green")
arrColors(3).Add("Yellow")
arrColors(4).Add("Black")

Response.Write(arrColors(0).ToString & "<br>")
Response.Write(arrColors(1).ToString & "<br>")
Response.Write(arrColors(2).ToString & "<br>")
Response.Write(arrColors(3).ToString & "<br>")
Response.Write(arrColors(4).ToString & "<br>")

The above code generates the following error:

Object reference not set to an instance of an object.

pointing to

arrColors(0).Add("Red")

Thanks,

Ron
 
Dim arrColors(5) As ArrayList
declares an ArrayList type
BUT - it does not create an instance of the type.
Dim arrColors As New ArrayList()
declares the Arraylist arrColors AND creates an instance.
You do not need to specify the number of elements as the ArrayList type
automatically expands as you add elements.

Whenever you get in trouble like this its a good idea to look at the
documentation, like so:
http://www.google.com/search?source=ig&hl=en&q=Arraylist+class

Cheers.
 
Dim arrColors(5) As ArrayList
declares an ArrayList type
BUT - it does not create an instance of the type.
Dim arrColors As New ArrayList()
declares the Arraylist arrColors AND creates an instance.
You do not need to specify the number of elements as the ArrayList type
automatically expands as you add elements.

Whenever you get in trouble like this its a good idea to look at the
documentation, like so:http://www.google.com/search?source=ig&hl=en&q=Arraylist+class

Cheers.
--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com











- Show quoted text -

Thanks, Peter, for your input but I am creating an instance. Please
have a look at the code I have shown in post #1. Note the line
immediately after I "Dim" the variable arrColors which is

arrColors(5) = New ArrayList

That line creates an instance, doesn't it?

Regards,

Ron
 
arrColors(0).Add("Red")

arrColours(0) is the object at position 0 in the array, however you have not
added anything to the array so this returns null (nothing) and you get your
error.

Do this instead;

arrColors.Add("Red")
arrColors.Add("Blue")
arrColors.Add("Green")
arrColors.Add("Yellow")
arrColors.Add("Black")
 
arrColours(0) is the object at position 0 in the array, however you have not
added anything to the array so this returns null (nothing) and you get your
error.

Do this instead;

arrColors.Add("Red")
arrColors.Add("Blue")
arrColors.Add("Green")
arrColors.Add("Yellow")
arrColors.Add("Black")

Thanks, Aidy, for your suggestion. Why doesn't

arrColors(0).Add("Red"),
arrColors(1).Add("Blue")

etc. add anything to the array? I couldn't exactly understand this.
Could you please explain me?

Also is it possible to ReDim & Preserve with ArrayList?

Thanks once again,

Regards,

Ron
 
Thanks, Aidy, for your suggestion. Why doesn't
arrColors(0).Add("Red"),
arrColors(1).Add("Blue")

etc. add anything to the array?

Basically an ArrayList is not an array, it is just a collection of objects.
With an array, if I create a 5 length array then I get a structure that can
hold 5 elements of my given type. So I can then do;

arr[0] = "Red"
arr[1] = "Blue"

etc.

When you create an ArrayList, you merely get an object that is an empty
collection, it has no objects in it so you can't do

arr[0] =

as there is nothing at position 0. For there to be something at position 0
you have to add it;

arr.Add("Red") -- this is the first thing added so will have position 0

You're just getting confused between arrays and the ArrayList object,
they're not the same. This syntax;

arrColors(0).Add("Red")

is the same as this

Dim o as Object
o = arrColours(0)
o.Add("Red")

What you're doing is getting the object at position 0 then calling that
object's Add method. As well as the problem above of there being no objects
in the array, if there *was* an object such as a string "Red" then you're
calling the Add method of that object which can give you errors if the
object doesn't support it. You are *not* calling the Add method of the
ArrayList, you are calling the Add method of the object at that position.
Also is it possible to ReDim & Preserve with ArrayList?

Again not really as they are just collections. The ArrayList will grow and
shrink in size as you add/remove elements.
 
Back
Top