• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

nullifying an object in a generics list collection

 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34651
365
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish,
Can you show the code that nullifies it?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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 Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49363
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:
Campbell: "It should"

Campbell ?
"Papa?"
"Nicole?"

Quite!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49363
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List is the only collection type that allows replacing values, but not while using the for-each loop.
 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic