我目前正在开发OpenOffice扩展程序。我使用的是Java 1.6和OpenOffice SDK 4.1.2。
如果我尝试创建一个javax.swing.JTextPane,我会在JTextPane的构造函数中得到一个NullpointerException。
public class Dialog extends javax.JFrame {
private final JTextPane jTextPane;
private final JTable jTable;
public Dialog() {
jTable = new JTable();
jTextPane = new JTextPane();
}
}
Dialog在另一个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();
...
}
}
}
}
}}
JTable的创建工作正常,但在下一行中我得到了一个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)
我希望有人知道导致此异常的原因。我试图在Java 1.7和1.8中运行扩展,但是存在同样的问题。
祝你好运
2016年6月16日更新: 如果我将Netbeans Palette上的JTexPane添加到Designer中,它可以工作。只有构造函数中的初始化失败。
答案 0 :(得分:2)
在没有崩溃的情况下,可能无法使用Swing执行此任务。而是使用com.sun.star.awt模块。完整示例位于http://api.libreoffice.org/examples/DevelopersGuide/examples.html#GraphicalUserInterfaces。
有关详细信息,请参阅Creating Dialogs at Runtime。
还有一个链接:This example确实使用了Swing。尝试看看是否出现同样的问题。
答案 1 :(得分:0)
我的脏解决方案是在try-catch中初始化JTextPane两次。因为在第二次通话中我不会得到例外。