• Post Reply Bookmark Topic Watch Topic
  • New Topic

ClassCastException  RSS feed

 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a fragment of code like this ..

Collection manifestCollection =
(Collection) manifestMessageTranslator.parseMessage(textMessage);
Iterator manifestIter = manifestCollection.iterator();
if (manifestCollection != null) {
while (manifestIter.hasNext()){
//System.out.println("The object is of type "+manifestCollection.iterator().next().getClass());
manifest = (Manifest)manifestIter.next();


Now I am getting a classcast exception on the last line. Please see the commented out line weher I was printing the class name of the object. It is properly priting it as class of Manifest. But I am still getting a classcastexception.

Please let me know what I am missing here.

Thanks very much,

Vasu
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of Manifest? There is a Manifest class in java.util.jar too.
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a custom class that is developed inhouse. When I browse on the name of the class the tooltip is rightly showing me the correct local class.

Thanks,

vasu
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You posted some rather mangled code:

First, you test for manifestCollection being null after applying a method to it!

But more to the point, your System.out check is very bad -- you create a new iterator
in it -- it will always only should you the class of the first object in the
iteration! What about the rest?

Try something like this:
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I have formated the code as you suggested. It is funny...it is going to the else part of the code and then it is printing the object as the class of Manifest. I am not getting it.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vasu maj:
Thanks. I have formated the code as you suggested. It is funny...it is going to the else part of the code and then it is printing the object as the class of Manifest. I am not getting it.


If you've arranged your code as Jeff suggested, then it shouldn't. Post your code and maybe someone can spot the problem.
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collection manifestCollection = manifestMessageTranslator.parseMessage(textMessage);
if (manifestCollection != null)
{ for(Iterator manifestIter = manifestCollection.iterator(); manifestIter.hasNext(); )
{ Object o = manifestIter.next();

if (o==null){
classname = "null";}

else
if (o instanceof Manifest) {manifest = (Manifest) o;
System.out.println("The class is of manifest");}
else { classname = o.getClass().getName();
System.err.println("The object is of type " + classname);
}

}
}
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is printing the System.out of the else part but as object of type Manifest
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have two classes named Manifest in different packages?

If that's not it, the only other thing I can think of is
if you have different Classloaders associated with
different thread groups. I seem to recall hazily that if that
is the case, the same .class file can be loaded twice
and considered to be two different classes. I doubt that is
what is happening, but if it is, this thread may need to be
moved to the Advanced forum!
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff, you are right. There are two different classes with the same name in diff packages and the one that is returned in the colection is different from what we are casting it to.

Thanks all,

Vasu
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!