2016-04-21 14 views
1

我正在嘗試創建一個編寫電子郵件消息的應用程序。當發送按鈕被按下時,應用程序打印出用戶輸入的收件人,抄送,密送,主題和消息。由於某些原因,當按鈕被按下時,它給了我一個錯誤「在線程異常‘的AWT - EventQueue的 - 0’ 這裏是我的代碼:爲編寫電子郵件消息創建GUI

import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 


    public class EmailWindow extends JPanel { 
    private JTextField to, cc, bcc, subject; 
    private JTextPane content; 
    private JButton send; 
    public EmailWindow() { 
     // Construct and add text fields for to, cc, bcc, and subject, followed 
     // by a Send button. You may use the createComponentWithLabel(...) 
     // utility method to construct a panel that includes a label and 
     // a text field, which can then be added to the EmailWindow panel. 
     // For example, 
     // add(createComponentWithLabel("Text", new JTextField(30)); 
     // would add a text field next to a label with the word "Text". 

     add(createComponentWithLabel("to", new JTextField(30))); 
     add(createComponentWithLabel("cc", new JTextField(30))); 
     add(createComponentWithLabel("bcc", new JTextField(30))); 
     add(createComponentWithLabel("subject", new JTextField(30))); 

     // The JTextPane class supports multi-line text. For a single line 
     // of content text, you could use another JTextField instead. 

     setBackground(Color.cyan); 
     content = new JTextPane(); 
     content.setPreferredSize(new Dimension(375, 200)); 
     send = new JButton("Send"); 
     send.addActionListener(new SendListener()); 
     add(content); 
     add(send); 
    } 

    // ----------------------------------------------------------------------- 
    // Utility method (which you may use in the constructor) that creates 
    // and returns a <code>JPanel</code> containing a <code>JLabel</code> 
    // next to an arbitrary component, such as a <code>JTextField</code>. 
    // ----------------------------------------------------------------------- 
    private JPanel createComponentWithLabel(String label, Component comp) { 
     JPanel p = new JPanel(); 
     p.setLayout(new BorderLayout()); 
     p.add(new JLabel(label, JLabel.RIGHT), BorderLayout.WEST); 
     p.add(comp, BorderLayout.CENTER); 
     return p; 
    } 

    // ----------------------------------------------------------------------- 
    // Listener class to be attached to the Send button. When the button 
    // is pressed, the contents of the to, cc, bcc, subject, and contents 
    // fields will be printed to standard out. 
    // ----------------------------------------------------------------------- 
    private class SendListener implements ActionListener { 

     public void actionPerformed(ActionEvent evt) { 
      System.out.println("To: " + to.getText()); 
      System.out.println("Cc: " + cc.getText()); 
      System.out.println("Bcc: " + bcc.getText()); 
      System.out.println("Subject: " + subject.getText()); 
      System.out.println("Message content: "+content.getText()); 
      System.out.println(content.getText()); 

     } 
    } 
} 

主要類:

import javax.swing.JFrame; 

    public class email { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     // Create a frame 
     JFrame frame = new JFrame("Compose Message"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     // Create an instance of EmailWindow and add it to the frame. 
     EmailWindow email = new EmailWindow(); 
     frame.getContentPane().add(email); 
     // Set a reasonable starting size for the frame. Note that we 
     // do not use pack() here, since doing so with the default layout 
     // manager would produce a very long frame. Other layout managers 
     // (which will be discussed in Chapter 6) would solve this problem 
     // in a more flexible way. 
     frame.setSize(425, 400); 
     // Show the frame. 
     frame.setVisible(true); 
     } 

} 

錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at hw6.EmailWindow$SendListener.actionPerformed(EmailWindow.java:59) 
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$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.security.ProtectionDomain$JavaSecurityAccessImpl.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$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) 
+0

請張貼異常的完整堆棧跟蹤。 – Berger

+0

添加異常 – begincoding123

回答

4

CCBCC主題JTextFieldnull

你從來沒有初始化它們,你可能想將它們作爲參數傳遞在這一部分:

add(createComponentWithLabel("to", new JTextField(30))); 
add(createComponentWithLabel("cc", new JTextField(30))); 
add(createComponentWithLabel("bcc", new JTextField(30))); 
add(createComponentWithLabel("subject", new JTextField(30))); 

我建議:

to = new JTextField(30); 
    cc = new JTextField(30); 
    bcc = new JTextField(30); 
    subject = new JTextField(30); 


    add(createComponentWithLabel("to", to)); 
    add(createComponentWithLabel("cc", cc)); 
    add(createComponentWithLabel("bcc", bcc)); 
    add(createComponentWithLabel("subject", subject)); 
+1

我明白了爲什麼我忘了初始化它們。這解決了這個問題。謝謝你的澄清 – begincoding123