cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

java.lang.ClassCastException

RK_10805837
6-Contributor

java.lang.ClassCastException

Hello,

 

I am facing an error when trying to cast version reference to Object Reference. The error is -

java.lang.ClassCastException: class wt.vc.VersionReference cannot be cast to class wt.fc.ObjectReference (wt.vc.VersionReference and wt.fc.ObjectReference are in unnamed module of loader 'app')

 

The code is-

String oid = (String) request.getParameter("oid");
final ReferenceFactory refFactory = new ReferenceFactory();
final ObjectReference reportRef = (ObjectReference) refFactory.getReference(oid);

 

Kindly suggest on how to resolve this,

Thanks

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @RK_10805837 

You can not do that. 

You need to crate new ObjectReference and assign the object to it.

Or rewrite the method you want to use.

 

ObjectReference objectReference = new ObjectReference();
objectReference.setObject((Persistable) reportRef.getObject());
objectReference.setKey(reportRef.getKey());

 

PS>it is not simple as I mentioned. The key from VersionReference can not be used in the ObjectReference object.  You should build the key from the object that is used in the VersionReference.

Usually something like  OR:wt.part.WTPart:105412210

PetrH 

View solution in original post

3 REPLIES 3

Hi @RK_10805837 

The getReference method returns VersionReference object so you should work with that type class

final VersionReference reportRef = (VersionReference ) refFactory.getReference(oid);

 

PetrH

Hi @HelesicPetr , 

The oid passed is already of the type VersionReference. I need to cast it to ObjectReference  because I have to pass it to a different method.

Can you please suggest some alternate way?

 

Thanks

Hi @RK_10805837 

You can not do that. 

You need to crate new ObjectReference and assign the object to it.

Or rewrite the method you want to use.

 

ObjectReference objectReference = new ObjectReference();
objectReference.setObject((Persistable) reportRef.getObject());
objectReference.setKey(reportRef.getKey());

 

PS>it is not simple as I mentioned. The key from VersionReference can not be used in the ObjectReference object.  You should build the key from the object that is used in the VersionReference.

Usually something like  OR:wt.part.WTPart:105412210

PetrH 

Top Tags