Adding an ArrayList to an ArrayList?

  • Thread starter Gustaf Liljegren
  • Start date
G

Gustaf Liljegren

It doesn't seem to work. I get the error:

System.NullReferenceException
Object reference not set to an instance of an object.

Here's the code:

public ArrayList _rar;
....
private void DoRAR(ArrayList array)
{
...
_rar.Add(array);
...
}

I tried ArrayList.AddRange() too, and I tried declaring _rar as a string
array. How am I supposed to add an array to an array?

Thanks,

Gustaf
 
M

Morten Wennevik

I'm not sure what you are trying to do. An arraylist takes object, so
basically anything can be put into an arraylist.

Did you intialize the arraylist?
Somewhere along the line, before calling Add you have to initialize
'array' with new ArrayList to create the list. Otherwise it's just a null
object, and get object reference not set to an instance of an object,
which basically means, array isn't pointing to an arraylist.
 
M

Morten Wennevik

Ah, upon rereading you code you may have failed to initialize _rar.
In your class constructor, write

_rar = new ArrayList();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top