JTextPane autoscrolling generating sporadic exceptions

Hi, I'm trying to autoscroll a chat JTextPane by doing something like the following:

try{

documentRef.insertString(documentRef.getLength(), newMessage, messageAttributes);

}

catch(BadLocationException e){

}

// Autoscroll the chat area to the end

java.awt.EventQueue.invokeLater(new Runnable(){

publicvoid run(){

myTextPane.setCaretPosition(documentRef.getLength());

}

});

documentRef is a "final" document object initialized just prior to that block of code in the same method, by using GetStyledDocument on the text pane. The code seems to work, but about half the time when I initialize the program and first write to the chat, I get the following giant exception stack trace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at javax.swing.text.FlowView$FlowStrategy.layoutRow(Unknown Source)

at javax.swing.text.FlowView$FlowStrategy.layout(Unknown Source)

at javax.swing.text.FlowView.layout(Unknown Source)

at javax.swing.text.BoxView.setSize(Unknown Source)

at javax.swing.text.BoxView.updateChildSizes(Unknown Source)

at javax.swing.text.BoxView.setSpanOnAxis(Unknown Source)

at javax.swing.text.BoxView.layout(Unknown Source)

at javax.swing.text.BoxView.setSize(Unknown Source)

at javax.swing.plaf.basic.BasicTextUI$RootView.setSize(Unknown Source)

at javax.swing.plaf.basic.BasicTextUI.modelToView(Unknown Source)

at javax.swing.text.DefaultCaret.repaintNewCaret(Unknown Source)

at javax.swing.text.DefaultCaret$1.run(Unknown Source)

at java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Sorry for the giant code/exception dump, but does anyone have ANY idea what might cause this and how to fix it? I'd like to have some confidence that (even if I make a change that makes it seem to go away) it won't happen in the future, since I'm assuming there's some programming mistake I'm making that's causing it.

[2902 byte] By [GTSchemera] at [2007-11-27 8:15:04]
# 1
I should also perhaps point out that the exception only generates if I'm trying to do the setCaretPosition tihng. Maybe it's something to do with the caret being set before the text is fully added? I have no idea. :(
GTSchemera at 2007-7-12 19:59:46 > top of Java-index,Desktop,Core GUI APIs...
# 2
Try to use SwingUtilities.invokeLater() rather than EventQueueRegards,Stas
StanislavLa at 2007-7-12 19:59:46 > top of Java-index,Desktop,Core GUI APIs...
# 3

Based on the few random lines of code posted, your approach looks fine. This works on a JTextArea, although I must admit I've never tried it on a JTextPane with attributes.

If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)", see http://homepage1.nifty.com/algafield/sscce.html, that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the "Code Formatting Tags", see http://forum.java.sun.com/help.jspa?sec=formatting, so the posted code retains its original formatting.

camickra at 2007-7-12 19:59:46 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thanks for the responses. I just tried for a while to duplicate in a simple program, but I haven't been able to.

I added code that made the intermittant exception go away, at least. If I check isVisible() inside the custom JFrame, in that invokeLater block that's trying to move the caret, it always returns false the first time because the JFrame has just been created/is being created, but already messages are being sent, apparently.

I don't know if the weird exception (which comes up some of the time, and the rest of the time the JTextPane appears to lose the first line of data or something and just have a blank line) was because I was trying to update the caret before everything in the window was created and visible, or what, but at least checking the visibility makes it seem to go away. Now let's hope I don't have to scroll on the first line of text sent to the window. :P

GTSchemera at 2007-7-12 19:59:46 > top of Java-index,Desktop,Core GUI APIs...
# 5

> Try to use SwingUtilities.invokeLater() rather than

> EventQueue

>

> Regards,

> Stas

SwingUtilities.invokeLater() does nothing other than call EventQueue.invokeLater().

Actually I prefer to use EventQueue as it is more obvious what is going on.

dwga at 2007-7-12 19:59:46 > top of Java-index,Desktop,Core GUI APIs...