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.

