2 Replies Latest reply on Oct 7, 2004 4:02 PM by 807587

    NPE in JEditorPane

      Hi all. This is an extremely weird bug, and I'm not sure how to fix it. I just want to know if anyone has any ideas.

      I'm using a JEditorPane to display help text. All is well except sometimes, the JEditorPane will throw an exception when I try to construct it (sometimes).

      This is how I use it:
      JEditorPane pane = new JEditorPane("text/html", getHelpHTML()); //getHelpHTLM returns a string, nothing fancy
      This is where it breaks (in JEditorPane) :
           * Establishes the default bindings of <code>type</code> to
           * <code>classname</code>.  
           * The class will be dynamically loaded later when actually
           * needed using the given <code>ClassLoader</code>,
           * and can be safely changed 
           * before attempted uses to avoid loading unwanted classes.
           * @param type the non-</code>null</code> content type
           * @param classname the class to load later
           * @param loader the <code>ClassLoader</code> to use to load the name
          public static void registerEditorKitForContentType(String type, String classname, ClassLoader loader) {
              getKitTypeRegistry().put(type, classname);  
           getKitLoaderRegistry().put(type, loader);   //Breaks here with NPE, loader is null
      Here's the stack trace:
           at java.util.Hashtable.put(Hashtable.java:393)
           at javax.swing.JEditorPane.registerEditorKitForContentType(JEditorPane.java:1135)
           at javax.swing.JEditorPane.registerEditorKitForContentType(JEditorPane.java:1117)
           at javax.swing.JEditorPane.loadDefaultKitsIfNecessary(JEditorPane.java:1182)
           at javax.swing.JEditorPane.getKitTypeRegistry(JEditorPane.java:1152)
           at javax.swing.JEditorPane.createEditorKitForContentType(JEditorPane.java:1077)
           at javax.swing.JEditorPane.getEditorKitForContentType(JEditorPane.java:975)
           at javax.swing.JEditorPane.setContentType(JEditorPane.java:869)
           at javax.swing.JEditorPane.<init>(JEditorPane.java:264)
      The thing is, this only occurs in my GUI code. I can't reproduce this with a unit test. The only thing I can think of is somehow I'm screwing around with the class loader, except I don't know anything about class loaders. Any thoughts on how to fix or how I accidently nulled out my classloader?

        • 1. Re: NPE in JEditorPane
          It looks to me as if the editor kit you need for text/html is not in the registry. I seem to recall that JEditorPane uses a plain text editor even though you want HTML. To do HTML you need to either explicitly set the editor kit or provide info in your getHelpHTML string to cause the JEditorPane to load the HTML kit. I used JPane because it is specifically designed for styled editing. You may get better results that way.

          This seemed to work OK for a read-only text pane in my project. I've added getHelpHTML()

          StyledDocument jDoc = new HTMLDocument();
          JTextPane jtp = new JTextPane(jDoc);
          jtp.setEditorKit(new HTMLEditorKit());

          Good luck
          • 2. Re: NPE in JEditorPane
            Thank you so much. I wouldn't have been able to catch that error without your help. It was a very weird bug.