2016-04-21 4 views
1

Ich versuche, eine Anwendung zum Verfassen einer E-Mail-Nachricht zu erstellen. Die Anwendung druckt die An, Cc, Bcc, Betreff und Nachricht von der Benutzereingabe aus, wenn die Sendetaste gedrückt wird. Aus irgendwelchen Gründen, wenn die Taste gedrückt wird, ist es mir ein Fehler „Exception in thread "AWT-Eventqueue-0" hier gab meine Codes:Erstellen von GUI zum Verfassen von E-Mail-Nachricht

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

     } 
    } 
} 

Hauptklasse:

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

} 

Fehler :

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

Bitte senden Sie den vollständigen Stack-Trace der Ausnahme. – Berger

+0

hinzugefügt Ausnahme – begincoding123

Antwort

4

Ihre -, cc, bcc, BetreffJTextField sind null.

Sie initialisieren sie nie, und Sie wollen wahrscheinlich sie als Parameter in diesem Teil zu übergeben:

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

Ich würde vorschlagen:

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

Ich sehe, warum ich vergessen habe, sie zu initialisieren. Dies behebt das Problem. Danke für die Abklärung – begincoding123

Verwandte Themen