Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ClassCastException: different results beteween instanceOf and getClass

807589Oct 6 2008 — edited Oct 6 2008
Hi,

I have a java based-simulation that contains the following code :
EDProtocol  prot = null;
boolean check=false;
			try {
				*prot = (EDProtocol) ev.node.getProtocol(pid);*
				prot.processEvent(ev.node, pid, ev.event);
                                *check= (ev.node.getProtocol(pid) instanceof DRingProtocol);*
			} catch (ClassCastException e) {
				throw new IllegalArgumentException("class: "+ ev.node.getProtocol(pid).getClass()+ "; is instance of DRingProtocol: "+ check);
			}
However, sometimes I get this weird error:
"Exception in thread "main" java.lang.IllegalArgumentException: Class: DRingProtocol; is instance of DRingProtocol; false"

How can "prot" not be an instance of its class???

Thanks

Mannous
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 3 2008
Added on Oct 6 2008
13 comments
211 views