Skip to main content

Cast Problems

1 reply [Last post]
giuseppe83
Offline
Joined: 2005-08-31
Points: 0

This peace of code throws a ClassCastException

public void setSelector(IRSimpleGA alg, Object[] args) throws UnrecoverableCommandException {
try {
//Object s=args[0];
//Class c=s.getClass();
//Class cl=c.getSuperclass();
Selector sel = (Selector)args[0];
alg.setSelector(sel);
} catch (Exception e) {
throw new UnrecoverableCommandException("IRSimpleGA.setSelector",
e.getMessage(), e);
}

}

But if I debug it with the three commented instructions decommented I can confirm that arg[0] is a subclass of Selector class. So why cast doesn't work??

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
destin
Offline
Joined: 2006-05-05
Points: 0

Why not just have args be an array of Selectors?
[code]public void setSelector(IRSimpleGA alg, Selector[] args) {
try {
alg.setSelector(args[0]);
} catch (Exception e) {
throw new UnrecoverableCommandException("IRSimpleGA.setSelector",
e.getMessage(), e);
}
}[/code]