• Post Reply Bookmark Topic Watch Topic
  • New Topic

NPE problem: Updating a JTree...any ideas?  RSS feed

 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranchers,

I have a JTree in a Swing based application. It is acted upon by 3 types of Threads:

1> The Default Main Program which creates the Tree; performs changes to TreeModel and consequently changes the Tree UI... I call tree.updateUI() to reflect the changes

2> Specific operational thread(s) ... which may alter the tree Model... they notify tree selection(s) changes to the Monitor...

3> Monitor thread which monitors operational thread(s)... and also updates the Tree UI... in synchronized blocks and synchronized methods...

Every once in a while I encounter NPE on tree.updateUI() calls. On further prodding the source code of tree.updateUI() [I use eclipse 3.1 as my IDE which lets me navigate to Java API's too)... the developer has a check where in he mentions in a comment that Swings is not meant for threaded programs and if model is changed from a different thread then such a check is valid...

I'll try to post the actual code snippet as soon as I encounter the piece of code again. But my question is if anyone has encountered similar situation; any workarounds fixes for this kind of NPE?

Thanks in advance
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you're violating the Swing contract regarding multithreading, and getting bit by it. Any changes to Swing components should always be made on the UI thread, using SwingUtilities.invokeLater() or invokeAndWait().
 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sheriff wrote:

Sounds like you're violating the Swing contract regarding multithreading, and getting bit by it. Any changes to Swing components should always be made on the UI thread, using SwingUtilities.invokeLater() or invokeAndWait().


Can you please elaborate a little bit on that?

If my thread is making a call like: (UIThreadObject).treeModel.reload() am I violating the Swing contract?

So if I include my call with in

Am I safe? Please correct me if I'm wrong

TIA
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and yes, I think. Does that invokeLater seem to work ok for you? I learned this the hard way, too, but my symptoms were more to do with incomplete painting than exceptions.
 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan,

Its been a steep learning curve for me too. I should have rephrased my problem. The root cause for NPE lies in incompleted painting rows.

Right now:

Different threads make changes to the treeModel without any synchronization;
But when it comes to updating the tree and presenting it to the user I'm invoking SwingUtilities.invokeLater and updating the UI as follows:

Is there a flaw in the logic?

I'm guessing UI thread gets a exclusive copy of treeModel which it uses to refesh UI; Hence it would be better put the updation of treeModel within a synchronized block.



any better ideas?

TIA
R
[ May 25, 2005: Message edited by: Ravi Sathish ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!