Search code examples
javaswingnullpointerexceptionjeditorpane

Making HyperlinkListener Work with JeditorPane NullPointerException


First post here sorry for my english, it's not my native language. I'm having trouble finding the error for the following code.

I'm trying to get the first JeditorPane to send me the value of the 4 checkboxes after clicking on submit and then change the content pane with the result of choosePage(). I get a NullpointerException:

What am I doing wrong?

Thanks in advance

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();
}
}

The main is just calling GlUi contructor here is the full 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)

Solution

  • I found the answer, i forgot to put action in the form.

    sb.append("<form>");
    

    should be

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