views:

913

answers:

5

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();
  }
 }
}
+1  A: 

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.

diciu
Oh yeah, totally: but then eclipse would be nice about it and tell me in the debug view that rules, name, or display is null. I would be able to figure it out. But the error I am getting is not telling me about those three, it is telling me about EventDispatchThread.run(), which I didn't write.
Ziggy
You've stuck a breakpoint in at the top of, and throughout, actionPerformed? What are the values at each step?
JeeBee
a lot of them come up with the value `line: not available`, because they are variables from not my code? My variables, rules, name, and display, aren't null. rules and display are big important non-null pieces, and name is null-checked right there in the method. right?
Ziggy
I bet the Eclipse debugger can set a breakpoint on throws. The wrost you could do is put a breakpoint in the NullPointerException constructor.
Tom Hawtin - tackline
+1  A: 

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.

Michael Borgwardt
but it isn't variables that I am trying to invoke that are null, the variables that are null are ones I can't see because they are in awt.event not in my code? No? Am I honestly wrong there?
Ziggy
I went through like you said, line by line. At some point it up and says EventDispatchTread.run() line: not available [local variables unavailable] and then won't tell me any more. it also says:but I do know exactly when now it happens, and that "stepping through" blew my mind!
Ziggy
You can get link source code for the API classes (it comes with the JDK) in eclipse. But sometimes the local variables are still hidden - I'm not sure what that depends on.
Michael Borgwardt
A: 

You might have forgotten to actually set an ActionCommand.

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

+1  A: 

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 :-))

agnul
This is a very useful insight! I see those hundreds of "line: not availables" and just think "hey, must not be my fault!" But actually, down below them was the problem.
Ziggy
+2  A: 

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.

Steve McLeod
Ziggy
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.
Steve McLeod
It was doing it: what happened I think was that since the error wasn't crashing the program (only the thread) the variables involved in the null went out of scope. When I went through the program step by step I was able to find the culprit. Thanks!
Ziggy