This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Threaded JTextArea Update Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Threaded JTextArea Update" Watch "Threaded JTextArea Update" New topic
Author

Threaded JTextArea Update

Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

So I'm trying to draw a (JTextArea, JTextPane, JTable, whatever will work) in a frame and update the information contained in that windows from several threads. Here is the code I have so far and all I"m getting is a NullPointer. I have to code to java 1.4 (Don't ask) and this code is hand jammed due to it being segregated so there may be little typo mistakes.



So that is where I start the monitor frame from main. The stuff that follows in main is what launches my other threads based on what is read from a config file. The next part is the actual frame itself, it's pretty rudimentary because I want to have the updating part working before I move on to making it prettier.



Now, the next part is an implementation of an interface that all my thread classes report to over a network. It has a queue and some other magic going on in the backgroud, but there could be several instances of it running at a time. More or less the interface is used to queue up sockets and perform work on them from a server thread, this part does the work that is queued to it and I define in main how many instances of it are allowed.



And this is where I'm stuck, I've tried using the SwingUtilities to queue up and append of String line to the Event Dispatcher and tried invoking the method with reflection(not sure if I did that right though) and a couple other things. I'm at a loss on how to put the String line in the JTextArea of my MonitorFrame class.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException




Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

Charles Burton wrote:Here is the code I have so far and all I"m getting is a NullPointer.


At what line of code? Show us the stacktrace.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
Why are you accessing a Swing Component from multiple threads? Swing is not thread safe, so you should only access anything from the Event Dispatch Thread.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

Charles Burton wrote: And this is where I'm stuck, I've tried using the SwingUtilities to queue up and append of String line to the Event Dispatcher...


I don't understand this either, really. You have a perfectly good updateEventArea(String) method which does exactly that. Why don't you just call it?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9993
    
    7

The methods append() and setText() are thread safe.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Maneesh Godbole wrote:The methods append() and setText() are thread safe.

Unfortunately, they're not, and the documentation error that says they are has been corrected for Java 7.
http://download.java.net/jdk7/docs/api/javax/swing/JTextArea.html


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

Are you saying the Javadoc has been incorrect for many Java versions already?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36563
    
  16
Rob Spoor wrote:Are you saying the Javadoc has been incorrect for many Java versions already?
Yes, he is.
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

Paul Clapham wrote:
Charles Burton wrote: And this is where I'm stuck, I've tried using the SwingUtilities to queue up and append of String line to the Event Dispatcher...


I don't understand this either, really. You have a perfectly good updateEventArea(String) method which does exactly that. Why don't you just call it?


When I try to call that method I get this.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at popup2.MonitorFrame$1.run(MonitorFrame.java:25)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597);
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

Paul Clapham wrote:
Charles Burton wrote:Here is the code I have so far and all I"m getting is a NullPointer.


At what line of code? Show us the stacktrace.


Oh, and up above is the stacktrace. Sorry that I didn't post it earlier, I have to hand jam everything.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

Line 25 is "eventArea.append(text);". Since the stack trace stops there you are dereferencing a null reference on that line. That leaves one option: eventArea is null. And that can only happen if updateEventArea() is called before go().
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

So I managed to fix the problem and it was an issue of scope. Here is the revised code showing how I fixed it. Again it's hand jammed so there may be small typos. Thread safety doesn't matter as much and I've tested it under somewhat heavy loads and the queue that handles all the incoming connections seems to behave fine with it.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Charles Burton wrote:Thread safety doesn't matter as much

Famous last words?
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

Darryl Burke wrote:
Charles Burton wrote:Thread safety doesn't matter as much

Famous last words?


Well, it seems to be working well so far. I changed it to using JList instead of JTextArea and thrown a couple hundred updates at it at a time and it seems to be doing fine. That's more than it'll see in real use. :P
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

It works fine at the moment on your own machine. But what about a later time? Or a different machine? Will it still work?
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

Rob Spoor wrote:It works fine at the moment on your own machine. But what about a later time? Or a different machine? Will it still work?


Indeed, I don't remember saying that I only tried it on my machine ;). I've actually tested it across more than just a few machines, but across a couple different architectures.
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

Darryl Burke wrote:
Charles Burton wrote:Thread safety doesn't matter as much

Famous last words?


Also, I should put that in a little context. The requestHandler class that you see up there is actually part of an interface that is backed by a work queue. Each thread sends their information via socket to the work queue and then the queue does all the thread handling.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2093
    
    7
I think people where commenting about your comment "thread safety doesn't matter as much". Thread safety does matter.

You don't see a problem because you have implemented the code in a thread safe manner. The invokeLater() makes the implementation thread safe.
Charles Burton
Ranch Hand

Joined: Jan 31, 2011
Posts: 39

Rob Camick wrote:I think people where commenting about your comment "thread safety doesn't matter as much". Thread safety does matter.

You don't see a problem because you have implemented the code in a thread safe manner. The invokeLater() makes the implementation thread safe.


I agree with you, I'm not trying to be a jerk or anything. That's why I added a little context to the statement, being nice in text is sometimes difficult and often comes across as clinical or defensive.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threaded JTextArea Update
 
Similar Threads
about threads.. there's something wrong though i can't seem to find it
Swing and Thread nightmare. Please help.
setText not updating promptly
JText Area..searching and replacing text
How to close this window?