473,473 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 8423
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RMD | last post by:
I need to be able to keep a list of object references, and null them out one by one at a later point in time. I realize this can be dangerous, but I have my reasons. I can't figure out, however,...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
3
by: vooose | last post by:
Consider a class, ClassA that is used to perform a task using multiple threads...ie ClassA aObj = new ClassA(param1); Thread t = new Thread(new ThreadStart(aObj.DoStuff));...
7
by: Ohad Asor | last post by:
Hello all, I have an ASP.NET page I've written using VS.NET2003, which have a ListBox in it. When I press a button in the form, I try to get the selected item in the list by calling...
10
by: Sek | last post by:
I have a property that returns a ArrayList object. On failure condition, is it right to return null or return an ArrayList object with zero elements?
1
by: dev24 | last post by:
Hi all, I am writing a simple method which when entered with an Int parameter performs an sql query, creates a result set, uses that resultset to get values from the object created by another...
6
by: dev24 | last post by:
Hi all, I am writing a simple method which when entered with an Int parameter performs an sql query, creates a result set, uses that resultset to get values from the object created by another...
2
by: yongw99 | last post by:
I have an arraylist which has 0 number of elements. When i cast it to a string array, what happens ? Will my string be a "null" value, or will I get a ArgumentNullException ? eg. ArrayList al...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.