• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threaded JTextArea Update

 
Ranch Hand
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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




 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The methods append() and setText() are thread safe.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you saying the Javadoc has been incorrect for many Java versions already?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Are you saying the Javadoc has been incorrect for many Java versions already?

Yes, he is.
 
Charles Burton
Ranch Hand
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Famous last words?
 
Charles Burton
Ranch Hand
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 3324
32
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
That feels good. Thanks. Here's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic