null in ArrayList

V

vooose

Consider an ArrayList that you add an object to:

ArrayList list = new ArrayList();
if(myObj != null)
list.Add(myObj);
else
throw new Exception("obj cannot be null");

Some time down the track, I end up with a null object in my ArrayList -
I don't know how this happens. At no point do I reassign (eg list =
<something null>

Any ideas how my arraylist ends up with a null object?
 
G

Guest

Hi vooose,
do you have a sample of the code which is doing this? When you assign the
object into the ArrayList a copy is made of the reference to object which is
pointed to by myObj. The only way you could end up with null in the
ArrayList is if you do assign null to one of the enteries.

Are you using multithreading in your app? One way could be that after
your check for null i.e.

if(myObj != null)

which returns true, then another thread comes along and sets myObj to null
which is then added to the list.

Mark Dawson
 
V

vooose

Hi Mark...Thanks for your reply. The actual arraylist is referenced in a
million different places (give or take :D) - I've gone through all the
ref's and most simply loop over the arraylist or do trivial functions. I
do use multi-threading as well and the situation you describe is
possible but I have a lock on an object prior to addition so no object
can access it at the same time (obviously this part could be flawed)

I just remembered something else - I had this problem on another
arraylist that was being accessed by multiple threads...it resulted in
"unpreditable" behavior - including making some items in the array equal
to null!

This is probably the issue - just back to debugging how multiple threads
are accessing the arraylist - what fun!
 
W

William Stacey [MVP]

Make sure your also taking the same lock object for writes to the list and
reads of the list everywhere it is accessed.

--
William Stacey [MVP]

| Hi Mark...Thanks for your reply. The actual arraylist is referenced in a
| million different places (give or take :D) - I've gone through all the
| ref's and most simply loop over the arraylist or do trivial functions. I
| do use multi-threading as well and the situation you describe is
| possible but I have a lock on an object prior to addition so no object
| can access it at the same time (obviously this part could be flawed)
|
| I just remembered something else - I had this problem on another
| arraylist that was being accessed by multiple threads...it resulted in
| "unpreditable" behavior - including making some items in the array equal
| to null!
|
| This is probably the issue - just back to debugging how multiple threads
| are accessing the arraylist - what fun!
|
| --
| Wal
| http://www.vooose.com
|
|
 
M

Michael C

vooose said:
Hi Mark...Thanks for your reply. The actual arraylist is referenced in a
million different places (give or take :D) - I've gone through all the
ref's and most simply loop over the arraylist or do trivial functions. I
do use multi-threading as well and the situation you describe is
possible but I have a lock on an object prior to addition so no object
can access it at the same time (obviously this part could be flawed)

Does putting a lock on the object also put a lock on the object reference?

Michael
 

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