What is this java.awt.event error?
Answer:
If you ever see these lines and fool yourself like me, this is what they mean.
Thread[AWT-EventQueue-0] (Suspended (exception NullPointerException))
EventDispatchTread.run() line: not available [local variables unavailable]
It's not that the variables are not available, because they are hiding behind a shroud of secrecy in the library, somewhere heck. No, they just went out of scope! It's still your fault, you still need to find zero and you can't blame the library. An important lesson!
Question:
One of the most frustrating things for me as a newbie is libraries! This is a love / hate relationship: on the one hand, they allow me to do what I usually don't understand how to do with code that I really understand, on the other hand, because I don't quite understand them, they sometimes throw a key in the code. which otherwise works fine! This is because I donโt understand the errors that can occur when using these libraries, because I didnโt write them, and because eclipse doesnโt give me much to go when one of the imports kicks in ...
So here's the problem: I was working with java.awt.event to handle a bunch of JButtons on the screen for this and that. I am getting an error when I use one of the buttons I made. Mistake:
Thread[AWT-EventQueue-0] (Suspended (exception NullPointerException))
EventDispatchTread.run() line: not available [local variables unavailable]
What does it mean? What could be the reason? I'm embarrassed to post the code, but if you can try to decipher my scary style, here is the method that seems to be causing this error.
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String name;
code...
if(cmd.equals("Play")) {
name = field.getText();
card = getCard(name);
if(card != null) {
if(rules.zoneHasCard(card, rules.hand)) {
display.updateStatusMessage(rules.play(card));
field.setText("");
display.updateHand(rules.zoneList("hand"));
display.updateDiscard(rules.zoneList("Discard")); // This is the error here! The discard Zone was empty!
}
else {
field.setText("You do not have " + card.getName());
field.selectAll();
}
}
else {
field.setText("That cardname is unused");
field.selectAll();
}
}
}
source to share
Welcome to the complexity of writing GUI code.
When you start a Swing program, a background thread called Thread Dispatch Thread is created. For example, when the user clicks on a JButton, the JButton creates and fires an event using this Event Dispatch Thread. Hence the name: this is the thread that dispatches events!
Your code:
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String name;
// more code...
}
is called by this Dispatch Thread event, so your code can handle the event.
Somewhere inside your code, you are trying to do something with a variable that is currently zero. The error message tells you, "Hey, while I'm running some code on the event dispatch thread, I ran into a NullPointerException" in your code .
Why don't you get more information? Perhaps you configured Eclipse to not include debug information on compilation?
Currently, I recommend adding some lines to your actionPerformed method to show the state of the variables:
System.out.println("field = " + field);
System.out.println("rules = " + rules);
System.out.println("display = " + display);
See if this shows you null values.
Even if the NullPointerException view comes from a library, the stack trace will show you which line of your code is called that library. But only if you've configured Eclipse to generate debug information.
Longer term, go through the Sun Swing tutorial to learn more about these issues.
source to share
You may have forgotten to set the ActionCommand.
There is a note in the ActionEvent API Doc about the possible null results of getActionCommand ().