Hi all and thanks for reading,
I'm quite shaky with Java at the best of times, but lately I'm trying to polish up a game of Scrabble I started for part of my University course. I'm using NetBeans 6.0.1 on Vista for the GUI, having hardcoded most of the logic myself in TextPad.
When I build and run the project in NetBeans, it gives me the following error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
at java.lang.Class.cast(Class.java:2990)
at org.jdesktop.beansbinding.Binding.convertForward(Binding.java:1312)
at org.jdesktop.beansbinding.Binding.getSourceValueForTarget(Binding.java:844)
at org.jdesktop.beansbinding.Binding.refreshUnmanaged(Binding.java:1222)
at org.jdesktop.beansbinding.Binding.refresh(Binding.java:1207)
at org.jdesktop.beansbinding.AutoBinding.tryRefreshThenSave(AutoBinding.java:162)
at org.jdesktop.beansbinding.AutoBinding.bindImpl(AutoBinding.java:199)
at org.jdesktop.beansbinding.Binding.bindUnmanaged(Binding.java:959)
at org.jdesktop.beansbinding.Binding.bind(Binding.java:944)
at org.jdesktop.beansbinding.BindingGroup.bind(BindingGroup.java:143)
at scrabble.GUI.initComponents(GUI.java:4020)
at scrabble.GUI.<init>(GUI.java:17)
at scrabble.GUI$5.run(GUI.java:4115)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
I've tried looking through forums and websites for this error but most agree it's a casting error relating to the object mentioned at
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:*OBJECT SHOULD BE MENTIONED HERE*
So, even if the exception occurs at the position referred to in the next line of the list, the code at
line 2990 of
java.lang.Class.cast(Class.java) is generated by NetBeans so shouldn't cause the problem, should it? Either way, the code is:
/**
* Casts an object to the class or interface represented
* by this <tt>Class</tt> object.
*
* @param obj the object to be cast
* @return the object after casting, or null if obj is null
*
* @throws ClassCastException if the object is not
* null and is not assignable to the type T.
*
* @since 1.5
*/
public T cast(Object obj) {
if (obj != null && !isInstance(obj))
throw new ClassCastException();
return (T) obj;
}
The only code I've added to NetBeans is for an ActionListener on a button designed to shuffle the current player's rack of tiles. I've added the code that creates a new instance of the Game object, as well as the instantiation of a new Player object, called "Steve" in this example:
private void shuffBtnActionPerformed(java.awt.event.ActionEvent evt) {
Game g = new Game();
g.addPlayer("Steve");
g.setActivePlayer(1);
System.out.println("Shuffle Letters");
newPlayer = g.getActivePlayer();
newPlayer.getRack().randomiseRack();
racktile1.setText(" "+newPlayer.getRack().getVecTiles().elementAt(0).toString());
racktile2.setText(" "+newPlayer.getRack().getVecTiles().elementAt(1).toString());
racktile3.setText(" "+newPlayer.getRack().getVecTiles().elementAt(2).toString());
racktile4.setText(" "+newPlayer.getRack().getVecTiles().elementAt(3).toString());
racktile5.setText(" "+newPlayer.getRack().getVecTiles().elementAt(4).toString());
racktile6.setText(" "+newPlayer.getRack().getVecTiles().elementAt(5).toString());
racktile7.setText(" "+newPlayer.getRack().getVecTiles().elementAt(6).toString());
}
Racktile is an object of type Tile, which is a custom object that extends JTextPane.
Sorry for the huge post, but like I said, I'm quite new to Java so have got quite lost in my first attempt at writing a useful program and I thought the more info I can give you all, the better you'll be able to help!
Thanks in advance,
Rhysicle
Edited by: rhysicle-dot-com on Jul 25, 2008 11:23 AM