File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes nullifying an object in a generics list collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "nullifying an object in a generics list collection" Watch "nullifying an object in a generics list collection" New topic
Author

nullifying an object in a generics list collection

manish ahuja
Ranch Hand

Joined: Oct 23, 2003
Posts: 312
Hi All,

Currently i have a scenario where in i get a generic list collection as an input parameter. In the method I scrutinize the collection in a for loop and based on some condition I want to nullify the custom object which is part of this collection.
I am setting this object to null but still when in another method i iterate through this collection and check something like this if(obj!=null) . Still the object which i nullified previously passes this if(obj!=null) condition.

Doesn't nullifying an object reference inside a for loop gets applied to the actual entry in the collection.


Let me know if I am missing something here.

-Manish
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Doesn't nullifying an object reference inside a for loop gets applied to the actual entry in the collection.

It should. Please show us how you did it.


[My Blog]
All roads lead to JavaRanch
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29219
    
134

Manish,
Can you show the code that nullifies it?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

> Doesn't nullifying an object reference inside a for loop gets applied to the actual entry in the collection.

No. You nullify a reference, and references are copied with method calls. If you want to remove an object from the list you should do list.remove(object), so the object is no longer in the list to be found again later on.


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

...
Doesn't nullifying an object reference inside a for loop gets applied to the actual entry in the collection.


Campbell: "It should"
Steve: "No"
Jeanne: "Can you show the code that nullifies it?"

Yeah, that's probably a good idea. :-)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Steve, nullifying a reference in a collection and removing an object in a collection are two different things. When you remove something from a collection, it's gone. Indexes are updated... There are times when you don't want to remove objects from a collection, but simply nullify its reference.

Campbell: "It should"

Campbell ?
manish ahuja
Ranch Hand

Joined: Oct 23, 2003
Posts: 312
Correct me if I am wrong. This is the first step

for (Model obj : modelCollection)
{

try {
processModel(obj);
}
catch(ModelException me)
{
log.error ("model exception raised for :" + obj.getId());
obj=null;
}
}

The processModel method will manipulate the model object passed. And in a different method when I iterate the collection I am able to see the results of the same.
There is one instance of the model object whose processing throws an exception I log the id of it in the catch block and nullify it.

But somehow this object passes the if (obj!=null) in another method where I iterate the above modified collection

another method :
-------------------
for (Model obj : modelCollection)
{
if(obj!=null)
{
log.debug(obj.getId()); // the one I nullified is available here.
doSomething(obj);
}
}


Thanks!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

manish ahuja wrote:Correct me if I am wrong.


OK -- you're wrong!

First of all, there's no such thing as "nullifying an object". A Java variable that refers to an object is a sort of "handle" or "label" for the object. There can be many, many handles that refer to the same object. Setting one to null does absolutely nothing to the object itself -- it just makes one of the (potentially many) handles not point to the object anymore.

When you have a collection, the collection contains handles thst refer to all the objects. When you iterate over a collection like

for (Model obj : modelCollection)

you're creating a new handle "obj" which refers to each object in turn. Setting "obj" to null does absolutely nothing to the object and absolutely nothing to the collection.

If you want to remove some objects from a collection, you must use an Iterator explicitly to iterate through the collection, and use the Iterator's "remove()" method to remove the object:



This will remove the "bad" object from the collection, as you desire.

[Jess in Action][AskingGoodQuestions]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

There is one instance of the model object whose processing throws an exception I log the id of it in the catch block and nullify it.

You are nullifying the local variable "obj". It doesn't affect what's in the collection.


Is there a reason to keep the element in the collection instead of removing it completely ? (refer to what Steve is saying)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Christophe Verré wrote:
Campbell: "It should"

Campbell ?
"Papa?"
"Nicole?"

Quite!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

Christophe Verré wrote:
Campbell: "It should"

Campbell ?


Haha! I had checked twice before making that post to make sure I didn't swap the 'it should' and 'show us the code' parts! Still got it wrong, Sorry :-)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Steve Luke wrote:
Christophe Verré wrote:
Campbell: "It should"

Campbell ?


Haha! I had checked twice before making that post to make sure I didn't swap the 'it should' and 'show us the code' parts! Still got it wrong, Sorry :-)
Apologies accepted
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

List is the only collection type that allows replacing values, but not while using the for-each loop.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
manish ahuja
Ranch Hand

Joined: Oct 23, 2003
Posts: 312
Removing the object is not an option as down the line in the code i need a count of the original size of the collection.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Then switch to List instead of Collection. Collection allows for duplicate elements to be removed (e.g. Set).

Say you pass a Set with 10 elements, and you want to replace 5 of those elements with null. Then you have a problem, because Set allows only one single null, and therefore the size will decrease to 6.

If you use List you can have duplicates, and also set certain elements to null:
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

manish ahuja wrote:Removing the object is not an option as down the line in the code i need a count of the original size of the collection.


Then, if you are using a List, you would use the ListIterator (not for loop) to iterate over the list, and replace the value with null:


If you are working with a Collection of unknown type, then you have to take into account that some collections aren't allowed to hold null, so you would have to remove the offending object and replace it with a NullObject version of your Model class (or something similar - it holds a place in the list but signals any readers that there is nothing actually there).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Steve Luke wrote:
manish ahuja wrote:Removing the object is not an option as down the line in the code i need a count of the original size of the collection.


Then, if you are using a List, you would use the ListIterator (not for loop) to iterate over the list, and replace the value with null:


If you are working with a Collection of unknown type, then you have to take into account that some collections aren't allowed to hold null, so you would have to remove the offending object and replace it with a NullObject version of your Model class (or something similar - it holds a place in the list but signals any readers that there is nothing actually there).

If the time between our posts wasn't that small I'd have thought you had copied my post
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: nullifying an object in a generics list collection
 
Similar Threads
AbstractTableModel NullPointerException
Making object null
foreach
How to crate Multiple ArrayList from one ArrayList
Assertions and sub-classing