Question

ANSWER:

If you ever see these lines and are mistified like I was, here's 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 unavailable because they are lurking behind a shroud of mystery in a library somewhere dank. No no, they just went out of scope! It's still your fault, you still have to find the null, and no you can't blame the library. Important lesson!

QUESTION:

One of the most frustrating things for me, as a beginner is libraries! It's a love/hate relationship: On the one hand they let me do things I wouldn't normally understand how to do with the code that I do understand, on the other hand because I don't completely understand them, they sometimes throw a wrench in code that is otherwise working fine! It's 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 a great deal to go with when one of imports starts acting up...

So here's the problem: I've been working with java.awt.event to handle a bunch of JButtons on the screen for this and that. I get an error when I use one of the buttons I've made. The error is:

Thread[AWT-EventQueue-0] (Suspended (exception NullPointerException))

EventDispatchTread.run() line: not available [local variables unavailable]

What does this mean? What could be causing it? I'm embarrassed to post code, but if you can stand to try to decipher my terrible style, here is the method that seems to cause this error to be thrown.

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();
        }
    }
}
Was it helpful?

Solution

Welcome to the complexity of writing GUI code.

When you run a Swing program, a background thread called the Event Dispatch Thread is created. When the user clicks on a JButton, for example, JButton creates and fires an event using this Event Dispatch Thread. Hence the name: it's the thread that dispatches events!

Your code:

public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        String name;

// more code...
}

is called by this Event Dispatch Thread, so your code can handle the event.

Somewhere within your code you are trying to do something with a variable that is currently equal to null. The error message is telling you, "hey while running some code on the event dispatch thread, I encountered a NullPointerException" in your code.

Why are you not receiving more info? Possibly you configured Eclipse not to include debug info when compiling?

For now, I recommend adding some lines to your actionPerformed method to show the state of variables:

System.out.println("field = " + field);
System.out.println("rules = " + rules);
System.out.println("display = " + display);

See if this shows you any nulls.

Even if the NullPointerException comes from a library, the stack trace will show which line of your code called that library. But only if you've configured Eclipse to generate debugging info.

In the longer term, work through the Sun's Swing Tutorial to learn more about these issues.

OTHER TIPS

Any method call on a null object will raise a null pointer exception.

In your code, rules, name or display could be null and cause an exception.

Use a debugger (such as the one included in the eclipse IDE) and set a breakpoint at the start of the actionPerformed() method, then step through it line by line to see when a variable you try to invoke a method on is null.

Just don't stop reading the stack trace after two lines. Somewhere in the stack trace you'll recognise the name of one of the classes/methods which you did write. Start looking there. (btw, people spend way to much time inside debuggers :-))

You might have forgotten to actually set an ActionCommand.

In the ActionEvent API Doc there's a note regarding possible null results of getActionCommand().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top