• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException with setCaretPosition?

 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

Essentially I'm using setting caret position to the length of my jtextpane document in a method I invoke everytime anything is added to the method. Thus my jtextpane auto-scrolls down. The problem is that I'm getting the following NullPointerException at seemingly random times, showing up at lines 116 and then 150. Here is the gist of my code at those two lines:

Line 116:
<blockquote>code:
<pre name="code" class="core">public static void scrollToBottom() {
textArea.setCaretPosition(textArea.getDocument().getLength()); } // Line 116
</pre>
</blockquote>

Line 150:
<blockquote>code:
<pre name="code" class="core">public void actionPerformed(ActionEvent av) {
synchronized (this) {
notify();
actionPerformed = true; }
try { styledDocument.insertString(styledDocument.getLength(),
"\n ", styledDocument.getStyle("ordinary"));
} catch (Exception ex) { ex.printStackTrace(); }
textField.selectAll();
scrollToBottom(); } // Line 150
</pre>
</blockquote>

And here is exception's details:
<blockquote>code:
<pre name="code" class="core">Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.text.DefaultStyledDocument.getParagraphElement(Unknown Source)
at javax.swing.text.StyledEditorKit$AttributeTracker.updateInputAttributes(Unknown Source)
at javax.swing.text.StyledEditorKit$AttributeTracker.caretUpdate(Unknown Source)
at javax.swing.text.JTextComponent.fireCaretUpdate(Unknown Source)
at javax.swing.text.JTextComponent$MutableCaretEvent.fire(Unknown Source)
at javax.swing.text.JTextComponent$MutableCaretEvent.stateChanged(Unknown Source)
at javax.swing.text.DefaultCaret.fireStateChanged(Unknown Source)
at javax.swing.text.DefaultCaret.changeCaretPosition(Unknown Source)
at javax.swing.text.DefaultCaret.handleSetDot(Unknown Source)
at javax.swing.text.DefaultCaret.setDot(Unknown Source)
at javax.swing.text.DefaultCaret.setDot(Unknown Source)
at javax.swing.text.JTextComponent.setCaretPosition(Unknown Source)
at navea.system.Display.scrollToBottom(Display.java:116)
at navea.system.Display.actionPerformed(Display.java:150)
</pre>
</blockquote>

So clearly the document or length of the document is null, right? But how could that possibly be? o.O And the randomness is what really confuses me. Sometimes it happens 100 lines into the jtextpane, sometimes not for quite a bit after, and sometimes not at all.

Thanks,
Alex
 
Campbell Ritchie
Marshal
Posts: 52574
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be the text area or the document which would be null, assuming that is actually where the Exception occurs. If the textArea and Document exist, then the length isn't null; it might be 0 but not null.
Try some print statements, eg insert this before line 116<blockquote>code:
<pre name="code" class="core">System.out.printf("textArea == null: %b, textArea.getDocument() == null: %b%n",
textArea == null, textArea.getDocument() == null);
</pre>
</blockquote>That should pick up potential nulls in line 116. If it prints out false twice and the Exception still occurs, then you will have to look elsewhere on the stack trace.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!