JDK-7116686 : JTextComponent.getText can return undocumented null
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2011-11-30
  • Updated: 2015-01-21
  • Resolved: 2015-01-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9Resolved
Related Reports
Duplicate :  
Description
An unreproducible NullPointerException in NetBeans can be traced to a JTextField returning null from getText(). The cause is probably the application incorrectly calling getText() from a non-EQ thread. Nonetheless, the possibility of a null return value seems to be endemic to the call, even if made from EQ:

        Document doc = getDocument();

Not documented thread-safe, so this ought to have been done on EQ. But unlikely in practice to matter, since the JTextComponent.model was not changing in this case, only its contents.

        String txt;
        try {
            txt = doc.getText(0, doc.getLength());

This is a race condition. Even if called from EQ, Document implementations in general permit modification of the text from any thread. (Even if modifications arising from user key type events happen to be limited to EQ, an application if free to make programmatic modifications from other threads while holding a write lock.)

        } catch (BadLocationException e) {
            txt = null;
        }
        return txt;

So now if the document became shorter between the calls to getLength and getText, null is returned. This possibility is not mentioned in

     * @return the text

which implies that a String representing the current full text of the document will always be returned (so long as the document itself is not null of course). Compare to the Javadoc for getSelectedText which explicitly discusses cases in which null may be returned.

Comments
- this is an issue reported against 7(7u), - there are now affected version 9 filed for this issue - 7u issues are transferred to Sustaining Nevertheless if someone have a report against 9 - please reopen and add affectedVersion 9 or 7u specific escalations might be reopen to Sustaining
10-08-2014

- this is an issue reported against 7(7u), - there are now affected version 9 filed for this issue - 7u issues are transferred to Sustaining Nevertheless if someone have a report against 9 - please reopen and add affectedVersion 9 or 7u specific escalations might be reopen to Sustaining
10-08-2014

SUGGESTED FIX Document doc = getDocument(); final AtomicReference<String> txt = new AtomicReference<>(""); doc.render(new Runnable() { @Override public void run() { try { txt.set(doc.getText(0, doc.getLength())); } catch (BadLocationException e) { assert false : e; } } }); return txt.get(); or slightly more efficiently, though less generally (consistent with many, though not all, methods in JTextComponent): Document doc = getDocument(); String txt; if (doc instanceof AbstractDocument) { ((AbstractDocument) doc).readLock(); } try { txt = doc.getText(0, doc.getLength()); } catch (BadLocationException e) { assert false : e; txt = ""; } finally { if (doc instanceof AbstractDocument) { ((AbstractDocument) doc).readUnlock(); } } return txt; or for the minimal change compliant with the spec: Document doc = getDocument(); String txt; try { txt = doc.getText(0, doc.getLength()); } catch (BadLocationException e) { txt = ""; } return txt;
30-11-2011

WORK AROUND Check for null return values, or somehow ensure that modifications might only happen from the same thread as is calling getText().
30-11-2011

PUBLIC COMMENTS http://netbeans.org/bugzilla/show_bug.cgi?id=205585
30-11-2011