JDK-6882559 : new JEditorPane("text/plain","") fails for null context class loader
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6u13
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris
  • CPU: x86
  • Submitted: 2009-09-16
  • Updated: 2017-01-11
  • Resolved: 2016-05-18
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 7 JDK 8 JDK 9 Other
7u121Fixed 8u102Fixed 9 b124Fixed openjdk7uFixed
Related Reports
Relates :  
Relates :  
At least with Java 6u13 (and apparently regardless of platform), executing
  public final class EditorTest {
    public static void main(String[] arguments) {
      new javax.swing.JEditorPane("text/plain", "");
fails with
  Exception in thread "main" java.lang.NullPointerException
  at java.util.Hashtable.put(Hashtable.java:394)
  at javax.swing.JEditorPane.registerEditorKitForContentType(JEditorPane.java:1329)
  at javax.swing.JEditorPane.registerEditorKitForContentType(JEditorPane.java:1311)
  at javax.swing.JEditorPane.loadDefaultKitsIfNecessary(JEditorPane.java:1389)
  at javax.swing.JEditorPane.getKitTypeRegistry(JEditorPane.java:1346)
  at javax.swing.JEditorPane.createEditorKitForContentType(JEditorPane.java:1271)
  at javax.swing.JEditorPane.getEditorKitForContentType(JEditorPane.java:1128)
  at javax.swing.JEditorPane.setContentType(JEditorPane.java:1019)
  at javax.swing.JEditorPane.<init>(JEditorPane.java:274)
  at EditorTest.main(EditorTest.java:4)

The problem appears to be that the null context class loader shall be put as a value into a Hashtable, which fails with a NullPointerException.  The documentation of JEditorPane does not directly state any requirements on the context class loader, and esp. for using the default "text/plain" it is not obvious why there should be any.

That a thread's context class loader is null does indeed happen "in real life," namely when a thread that was spawned outside the JVM is attached to the JVM via JNI; according to <http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/invocation.html#attach_current_thread> "[w]hen a thread is attached to the VM, the context class loader is the bootstrap loader," and in turn "[s]ome implementations may use null to represent the bootstrap class loader," according to <http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html#getParent()>).

OpenOffice.org is having a problem with this, see <http://qa.openoffice.org/issues/show_bug.cgi?id=102164#desc8>.

If there's a clear reason, for addressing this on JDK 6 then we can consider a backport, but at this time there are no plans to address for JDK 6.

Review: http://mail.openjdk.java.net/pipermail/swing-dev/2016-May/005916.html

The problem is that JTextPane during initialization saves Thread.contextClassloader which is null, it saves it for lazy initialization(later it will init some other data), but class loader is a null and Hashtable, which is used to store class loader, doesn't support a null value as result we get an exception. However JTextPane doesn't need to save Thread.contextClassloader, it's optional: http://hg.openjdk.java.net/jdk9/client/jdk/file/fb38b0925915/src/java.desktop/share/classes/javax/swing/JEditorPane.java#l1192 so it will work fine even if ClassLoader is null. To fix this I wrapped a classloader into Optional before to put it into Hashtable.