471,319 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

null in ArrayList

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[i] =
<something null>

Any ideas how my arraylist ends up with a null object?
--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***
Feb 20 '06 #1
5 8314
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
--
http://www.markdawson.org
"vooose" wrote:
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[i] =
<something null>

Any ideas how my arraylist ends up with a null object?
--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***

Feb 20 '06 #2
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

*** Sent via Developersdex http://www.developersdex.com ***
Feb 20 '06 #3
"vooose" <no****@microsoft.com> wrote in message
news:ui**************@tk2msftngp13.phx.gbl...
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
Feb 20 '06 #4
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]

"vooose" <no****@microsoft.com> wrote in message
news:ui**************@tk2msftngp13.phx.gbl...
| 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
|
| *** Sent via Developersdex http://www.developersdex.com ***
Feb 20 '06 #5
Hi,


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

Take a look at http://www.yoda.arachsys.com/csharp/...ckchoice.shtml
it's a very good article about locking options.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 20 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by RMD | last post: by
5 posts views Thread by David Sworder | last post: by
3 posts views Thread by vooose | last post: by
7 posts views Thread by Ohad Asor | last post: by
reply views Thread by rosydwin | last post: by

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.