使HyperlinkListener与JeditorPane一起使用NullPointerException

时间:2014-11-25 16:04:22

标签: java swing nullpointerexception jeditorpane

首先发帖抱歉我的英语,这不是我的母语。我无法找到以下代码的错误。

我正在尝试让第一个JeditorPane在点击提交后向我发送4个复选框的值,然后使用choosePage()的结果更改内容窗格。我得到了NullpointerException

我做错了什么?

提前致谢

private JEditorPane edit = new JEditorPane();
private String[] pages = {"Login","EFac","Home"};
private StringBuilder sb = null;
private JFrame mainFrame = null;

public GlUI(){
    edit = new JEditorPane();
    mainFrame=new JFrame("TestScenar");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.getContentPane().add(new JScrollPane(edit));

    edit.setEditable(false);

    HTMLEditorKit kit = new HTMLEditorKit();
    edit.setEditorKit(kit);
    kit.setAutoFormSubmission(false);

    edit.addHyperlinkListener(new HyperlinkListener()
    {                           
        @Override
        public void hyperlinkUpdate(HyperlinkEvent e)
        {
            if (e instanceof FormSubmitEvent)
            {
                System.out.println(e);
            }
        }
    });

    Document doc = kit.createDefaultDocument();
    edit.setDocument(doc);
    edit.setText(pageChoice());

    mainFrame.setSize(800,600);
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setVisible(true);
}

private String pageChoice() {
    sb = new StringBuilder();
    sb.append("<html>");
    sb.append("<body>");

    sb.append("<form>");
    for(String page:pages){
        sb.append("<input type='checkbox' name=" + page + "/>" + page + "</br>");
    }
    sb.append("<input type='submit' value='Submit'>");
    sb.append("</form>");

    sb.append("</html>");
    sb.append("</body>");
    return sb.toString();
}
}

主要是调用GlUi构造函数 这是完整的stackTrace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.text.html.FormView.submitData(Unknown Source)
    at javax.swing.text.html.FormView.actionPerformed(Unknown Source)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(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$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.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)

1 个答案:

答案 0 :(得分:1)

我找到了答案,我忘了在表格中采取行动。

sb.append("<form>");

应该是

sb.append("<form action=\"#\">");`