473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ArrayList

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

Oct 16 '07 #1
3 1708
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")
Oct 16 '07 #2
On Oct 16, 3:26 am, "Aidy" <a...@xxnoemailxx.comwrote:
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")
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

Oct 16 '07 #3
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.
Oct 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a...
3
by: george r smith | last post by:
I am trying to create an arrayList that contains multiple arrayLists. My code attempt is below. The question I have is how can I get away from creating another pAttribute list than can be added to...
3
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
6
by: gane kol | last post by:
Hi, I have a code that creates a datatable from an arraylist, but i am getting an error in casting in for (int intRow = 0; intRow < alLCPlist.Count; intRow++) { DataRow drow =...
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello My pgm1 (User Interface Level) passes an empty ArrayList to pgm2 (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate the ArrayList. Question1: When pgm2 gets...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.