473,699 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ 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 8468
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
"unpreditab le" 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****@microso ft.com> wrote in message
news:ui******** ******@tk2msftn gp13.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****@microso ft.com> wrote in message
news:ui******** ******@tk2msftn gp13.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
| "unpreditab le" 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
1712
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, how to keep what is essentially a reference to a reference. I want to store my objects in an ArrayList, then loop through the array list and null out the original reference I was given when I added it to my Array List. Below is some example...
5
24712
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 s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
3
1847
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)); anArrayList.Add(aObj); The above may be called in many places, so the size of anArrayList grows.
7
11188
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 ListBox.SelectedItem. And it returns null, even when I select an item. Even when I try to read the value when I get the SelectedIndexChanged event, it still doesn't give me the correct answer.
10
4732
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
5331
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 method and display the result as an arraylist. The method and the object are as below: public static ArrayList getProjects(int userID) throws SQLException, Exception { ArrayList arrayList = null; ...
6
4187
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 method and display the result as an arraylist. The method and the object are as below: public static ArrayList getProjects(int userID) throws SQLException, Exception { ArrayList arrayList = null; getProjectsStmt.setInt(1, userID);
2
1576
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 = new ArrayList(); String myAr = null; ...... ............ myAr = (String)al.ToArray(typeof(string));
0
8705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8628
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8899
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6550
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5884
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.