ArrayList in Hashtable

M

Matthias Kwiedor

Hi!


What i want to do, is to put an ArrayList into a Hashtable. So i can access
the ArrayList fast over the key-indexing of the Hashtable.

What i did is to put the ArrayList into the Hashtable

hash[key] = arraylist


When i get the ArrayList back

ArrayList array = (ArrayList)hash[key]

But when i try to access array (for example array.Count) i get a null
reference exception.

Can someone help me out of there.



Regars



Matthias
 
N

Nicholas Paldino [.NET/C# MVP]

Matthias,

The only way that this could happen (assuming that hash is the same
hashtable in both instances) is if the key that was used to fetch the
ArrayList is different than the key that was used to get the ArrayList. Are
you sure they are the same?

Also, are you sure that nothing modified hash or the value pointed to by
key between the time that you set it and the time you try to access it?
 
J

Jon Skeet [C# MVP]

Matthias Kwiedor said:
What i want to do, is to put an ArrayList into a Hashtable. So i can access
the ArrayList fast over the key-indexing of the Hashtable.

What i did is to put the ArrayList into the Hashtable

hash[key] = arraylist


When i get the ArrayList back

ArrayList array = (ArrayList)hash[key]

But when i try to access array (for example array.Count) i get a null
reference exception.

Can someone help me out of there.

Did you ever actually initialise arraylist to have a reference to an
actual instance of ArrayList, rather than just being null?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
R

Robert Hooker

This can happen if the object which is your key overides Equals() and
returns false erroneously.

Try this:

bool valid = hash.ContainsKey(key);

What is "valid" - if its false - then it's not finding a match to your key.
 

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