Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.KeyS  RSS feed

 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Please excuse if this is in the wrong section but as the beginning of the error said Thread i figured probably the best place to put it.

I am currently working on a Java Chat program, and have been encountering this error that is driving me insane!!

It only seems to happen now and again, most of the time the program works without a problem but then all of a sudden it will begin to produce this error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.KeyStroke

Has anyone else come accross this problem?

If not has anyone got any idea y this problem is occuring?

Thanks in advance,

John
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a Swing error; it's telling you on what thread the exception occurred, but that doesn't make it a threading problem.

Do you see a whole stack trace? That would allow you to see what code is running when the error occurs. Hopefully, it's your own code and you can find out who is making the bad cast.

I'm going to move this to the Swing/AWT forum for further discussion.
 
Campbell Ritchie
Marshal
Posts: 55711
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to check the line where the exception occurred, which you can find in a few minutes by following the printed stack trace. You have probably got all the details you require in the heading of the thread (thank you for giving such a helpful title).
Go to the line in questions where you might find something like this:-or maybe:-What class casting does is to take an object and apply the name of a class to it. BUT that object has to be an instance of that class. Otherwise the exception.

If you look in the API specification for KeyStroke, you find it extends AWTKeyStroke which extends Object. I couldn't find a known subclass for KeyStroke on the API, but you might have one yourself, let's call it JohnsKeyStroke. So you can cast a KeyStroke or a JohnsKeysStroke to KeyStroke. You can also cast a KeyStroke to AWTKeyStroke or Object, but both those casts are rather pointless.
If you have another subclass of AWTKeyStroke, let's say John'sAWTKeyStroke, you can't cast that to KeyStroke.

I hope I have made things clearer rather than confused you.

CR
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol, i am still a little confused but it is just due to the randomness of the error. But i will continue my research into it.

Here is what was printed after the error, im not entirely sure where to look tbh.

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.KeyStroke
at java.util.TreeMap.compare(TreeMap.java:1093)
at java.util.TreeMap.put(TreeMap.java:465)
at java.util.TreeSet.add(TreeSet.java:210)
at javax.swing.JComponent.getManagingFocusBackwardTraversalKeys(JComponent.java:409)
at javax.swing.JTextArea.<init>(JTextArea.java:203)
at javax.swing.JTextArea.<init>(JTextArea.java:118)
at client.clientChat.initComponents(clientChat.java:58)
at client.clientChat.<init>(clientChat.java:37)
at client.clientGUI.bChatActionPerformed(clientGUI.java:209)
at client.clientGUI.access$100(clientGUI.java:18)
at client.clientGUI$2.actionPerformed(clientGUI.java:134)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Hopefully this will aid in people understading what is wrong, all the things being traced to my program i will put the code for below:

at client.clientChat.initComponents(clientChat.java:58)

tConvo = new javax.swing.JTextArea();

at client.clientChat.<init>(clientChat.java:37)

//call from within the constructor
initComponents();

at client.clientGUI.bChatActionPerformed(clientGUI.java:209)



at client.clientGUI.access$100(clientGUI.java:18)

public class clientGUI extends javax.swing.JFrame
implements Runnable

at client.clientGUI$2.actionPerformed(clientGUI.java:134)



Sorry for the lenght of this message, its just i have no idea where to move forward with this error....

Any ideas would be gr8ly appreciated,

Thank you

John
[ June 04, 2006: Message edited by: John Bartlett ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!