views:

32

answers:

1

I have a Java software that was recently integrated into another Java software (which I will call "external" software). We use listeners and call back mechanisms for "communication" between two softwares.

Creators of the "external" software say that they get a NullPointerException because of some EDT violations in my code. Can it be the case?

+4  A: 

EDT violations certainly can cause behavior like this, provided these components interact with Swing components in some way (say they are attached as listeners on components). Of course, you could turn the tables on them and ask them if they have assertions on these methods and that if you enable assertions will the code clearly show where the EDT rule is violated.

Edit (in response to comment):

The basic rule of thumb with the EDT is that whenever you start a new thread do not touch any Swing component (or anything that touches a Swing component, such as a Model) without wrapping that code in a Runnable and calling SwingUtilities.invokeLater(Runnable). It takes dicipline and some extra design effort, but it is definitely necessary in any serious application.

If you have a serious amount invested in syncronous behavior (such as poping up a dialog and waiting for an answer) you can call SwingUtilities.invokeAndWait(Runnable), but you should try to avoid the need for that as much as possible. Also, make sure you call that method while not on the EDT, it doesn't work otherwise.

One way to start getting such code under control is to use asserts like this:

   assert (EventQueue.isDispatchThread())

whereever you have code that touches the GUI and run with asserts enabled. That way you will see exact code paths that are touching Swing components incorrectly.

Yishai
My code can contain EDT violations but it works fine on my computer (no crash, no freezing). So, I hoped that I do not need to fix my code.
Roman
@Roman, unless the code is only intended to ever run on your machine, it looks like you will have to fix it. Many problems will happen on other machines that don't appear on yours. Threading issues can be subtle.
Yishai
@Yishai, do you know some simple description of what EDT violations are (why they can happen) and how I can remove them?
Roman
@Roman, As someone who has no clue, I Googled and read. This looks promising: http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html
Tim Bender