1

I'm currently developing an Extension for OpenOffice. I'm using Java 1.6 and the OpenOffice SDK 4.1.2.

If I try to create a javax.swing.JTextPane, I get a NullpointerException in the Constructor of JTextPane.

public class Dialog extends javax.JFrame {

private final JTextPane jTextPane;
private final JTable jTable;



    public Dialog()  {
         jTable = new JTable();
         jTextPane = new JTextPane();

    }
}

The Dialog is initialized in another Thread:

public class DialogManager {
private static JournalDialog journalDialog;

public void showDialog() {




Thread startThread = new Thread(new Runnable() {


        @Override
        public void run() {

            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                     dialog = new Dialog();
                     ...
                }
            }
        }
 }
}}

The creation of JTable works fine, but in the next line I get a Nullpointerexception

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.util.Hashtable.put(Unknown Source)
at javax.swing.JEditorPane.registerEditorKitForContentType(Unknown Source)
at javax.swing.JEditorPane.registerEditorKitForContentType(Unknown Source)
at javax.swing.JEditorPane.loadDefaultKitsIfNecessary(Unknown Source)
at javax.swing.JEditorPane.getKitTypeRegistry(Unknown Source)
at javax.swing.JEditorPane.getEditorKitClassNameForContentType(Unknown Source)
at javax.swing.JTextPane.<init>(Unknown Source)
at .gui.Dialog.<init>(Dialog.java:159)
at .gui.DialogManager$6$1.run(DialogManager.java:334)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(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)

I hope someone has an idea what causes this exception. I tried to run the extension in Java 1.7 and 1.8, but there is the same issue.

Best regards

Update 16.08.2016: If I add a JTexPane over the Netbeans Palette into the Designer, it works. Only the initialisation in the constructor fails.

  • You know the NullPointerException drill: which line is `Dialog.java:159`? – Hovercraft Full Of Eels Aug 12 '16 at 14:05
  • its the line where "jTextPane = new JTextPane();" is called. – MountRushmore Aug 12 '16 at 14:11
  • and 'DialogManager.java:334' is 'dialog = new Dialog();' – MountRushmore Aug 12 '16 at 14:16
  • Consider trying to pare down your code to see if you can isolate the problem -- to see which parts of your code are essential for reproducing the problem. – Hovercraft Full Of Eels Aug 12 '16 at 14:24
  • Are you using any hash tables in your program? If so, are you putting any `Dialog` objects into them? The line `at java.util.Hashtable.put(Unknown Source)` of the trace concerns me. – Matthew Diana Aug 12 '16 at 14:30
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Aug 12 '16 at 14:33
  • Thanks for your help. I will try to isolate the problem. I'm not using a Hashtable, I think the call occur in a swing component. – MountRushmore Aug 16 '16 at 08:14

2 Answers2

2

It may not be possible to use Swing for this task without crashing. Instead, use the com.sun.star.awt module. Complete examples are at http://api.libreoffice.org/examples/DevelopersGuide/examples.html#GraphicalUserInterfaces.

For more information, see Creating Dialogs at Runtime.

One more link: This example does use Swing. Try it to see if the same problem occurs.

Jim K
  • 12,824
  • 2
  • 22
  • 51
  • @Hovercraft Full Of Eels: I edited my question, so hopefully it is clearer now. It may have to do with a conflict between the way UNO handles events versus the way Swing handles them. The standard recommendation is to use UNO instead of Swing for Java macro dialogs. – Jim K Aug 12 '16 at 18:13
  • Thanks for your hints. Why I cannot use Swing for my GUI? In the wiki they don't say, that I have to use com.sun.star.awt My complete extension is written with swing, but only the JTextPane is making problems. https://wiki.openoffice.org/wiki/Documentation/DevGuide/GUI/Graphical_User_Interfaces – MountRushmore Aug 16 '16 at 08:17
1

My dirty solution is to initialize the JTextPane twice within a try-catch. Because on the second call i don't get an exception.