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();
        }
    }
}

      

+1


source to share


5 answers


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.

+4


source


Any method call on a null object throws a null pointer exception.



In your code, rules, name or display can be empty and throw an exception.

+1


source


Use a debugger (like the one included in the Eclipse IDE) and set a breakpoint at the beginning of the actionPerformed () method, then traverse the line to see when the variable you're trying to call the method isn't null.

+1


source


Just don't stop reading the stack trace after two lines. Somewhere along the stack trace, you will recognize the name of one of the classes / methods you are writing. Start looking there. (by the way, people spend a lot of time on debuggers :-))

+1


source


You may have forgotten to set the ActionCommand.

There is a note in the ActionEvent API Doc about the possible null results of getActionCommand ().

0


source







All Articles