Home arrow static arrow Java Programming [Archive] - Classcast-Problems
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Classcast-Problems
This topic has 3 replies on 1 page.

Posts:18
Registered: 12/12/02
Classcast-Problems  
Aug 5, 2004 4:16 AM



 
I definitly sure that the ISelection is a TableRow which can be seen from the output below. I'm not sure what the [-brackets mean, can anybody help me out here, I must be doing something fundametly wrong.
--------------8<---------------
viewer_.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged( SelectionChangedEvent evt ) {
ISelection selection = evt.getSelectionProvider().getSelection();
System.out.println("SELECTION CHANGED!!!!");
System.out.println( evt.getSelectionProvider().getSelection() );
System.out.println( evt.getSelectionProvider());

try {
TableRow row = (TableRow)selection // THROWS CLASS-CAST EXCEPTION;
} catch( ClassCastException e ) {
System.out.println( "E-Message: " + e.getMessage() );
}
}
});
--------------8<---------------

Output:
--------------8<---------------
SELECTION CHANGED!!!!
[at.bestsolution.swtforms.provider.tableutils.TableRow@dc6a77]
org.eclipse.jface.viewers.TableViewer@d1e89e
E-Message: null
--------------8<---------------

Tom
 

Posts:1,995
Registered: 12/10/00
Re: Classcast-Problems  
Aug 5, 2004 4:33 AM (reply 1 of 3)



 
You can check the real class of selection (if it is not null) by printing
selection.getClass().getName();


HTH
Mike
 

Posts:668
Registered: 9/2/03
Re: Classcast-Problems  
Aug 5, 2004 4:37 AM (reply 2 of 3)



 
You can try using the instanceof operator before class-casting (that's what I usually do):
if(selection instanceof TableRow) {  TableRow row = (TableRow)selection;} else {  System.out.println("Oh goody! We're screwed now!");}


Hope this helps a bit.
 

Posts:2,909
Registered: 13.8.2003
Re: Classcast-Problems  
Aug 5, 2004 4:50 AM (reply 3 of 3)



 
[ is an indication of an array ([[ for 2 dimensional, [[[ for 3 dimensional etc. may not be true on all VMs/platforms). Maybe you're getting an array of objects when you're thinking that you get only one?

Didn't read the question very well so answer might be off.
 
This topic has 3 replies on 1 page.