java.lang.NumberFormatException:对于输入字符串:" null"在尝试使用netbeans发送电子邮件时

时间:2014-03-19 12:09:15

标签: java email netbeans

我正在尝试使用netbeans中的电子邮件客户端发送电子邮件,但我在我的代码中获得了numberformatexception。这是我的代码(有三个类):

EmailClient.java

package sendemail;

    public class EmailClient extends javax.swing.JFrame {

        SendMail sm=new SendMail();
        Settings set=new Settings();
        public EmailClient() {
            initComponents();
        }


        @SuppressWarnings("unchecked")
        +Generated code                     

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
           // TODO add your handling code here:
            sm.setVisible(true);
        }                                        

        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
        }                                        

        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
        }                                        

        private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
            set.setVisible(true);
        }                                          

        public static void main(String args[]) {
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new EmailClient().setVisible(true);
                }
            });
        }

        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JMenu jMenu4;
        private javax.swing.JMenu jMenu5;
        private javax.swing.JMenuBar jMenuBar2;
        private javax.swing.JMenuItem jMenuItem2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel3;
        private javax.swing.JPanel jPanel4;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        // End of variables declaration                   
    }

Settings.java

    package sendemail;
    import javax.swing.*;
    import java.awt.*;

    public class Settings extends javax.swing.JFrame {

        public String uname;
        public String pass;
        public String smtpserver;
        public String  port;
        /**
         * Creates new form Settings
         */
        public Settings() {
            initComponents();
        }

        public String getUname() {
            return uname;
        }

        public String getPass() {
            return pass;
        }

        public String getSmtpserver() {
            return smtpserver;
        }

        public String getPort() {
            return port;
        }



        @SuppressWarnings("unchecked")
        +Generated Code                   

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
           uname=jTextField1.getText().toString();
            pass=jPasswordField1.getPassword().toString();

            smtpserver=jComboBox1.getSelectedItem().toString();
            port=jComboBox2.getSelectedItem().toString();

            if(uname.equals("") || pass.equals("") || smtpserver.equals("") || port.equals("") )
        {
                JOptionPane.showMessageDialog(this,"All Fields are mandatory");
        }                                        
        else
            {
                setVisible(false);
            }

        }                                        

        private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
        }                                          

        public static void main(String args[]) {
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Settings().setVisible(false);
                }
            });
        }

        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JComboBox jComboBox2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPasswordField jPasswordField1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   

    }

SendMail.Java

package sendemail;

    import java.util.Properties;
    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import javax.swing.JOptionPane;
    import java.awt.*;
    import javax.mail.*;
    import javax.mail.MessagingException;


    public class SendMail extends javax.swing.JFrame {

        Settings setfrm=new Settings();
        String subject;
        String from;

        public SendMail() {
            initComponents();
        }


        @SuppressWarnings("unchecked")
        +Generated Code                    

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            try
            {
            final String user=setfrm.getUname();
            final String password=setfrm.getPass();
            String portnum=setfrm.getPort();
            String smtpname=setfrm.getSmtpserver();
            String to=jTextField1.getText();
            subject=jTextField2.getText();
            Properties properties=new Properties();
            properties.put("mail.smtp.host",smtpname.toString());
            properties.put("mail.smtp.socketFactory.port",portnum.toString());
            properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
            properties.put("mail.smtp.port",portnum.toString());
            properties.put("mail.smtp.auth","true");
            Session session=Session.getDefaultInstance(properties,
               new javax.mail.Authenticator() {
                   protected PasswordAuthentication getPasswordAuthentication(){
                    return new PasswordAuthentication(user,password);

               }

               }

               );
            MimeMessage message=new MimeMessage(session);
            message.setFrom(new InternetAddress(user));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(jTextField1.getText().toString()));
            message.setSubject(subject);
            message.setText(jTextArea1.getText());
            Transport.send(message);
            JOptionPane.showMessageDialog(null,"message sent");
            }
            catch(MessagingException mex)
            {
                JOptionPane.showMessageDialog(null,mex);
            }
        }                                        

        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
        }                                        

        private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
        }                                           

        public static void main(String args[]) {
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new SendMail().setVisible(true);
                }
            });
        }

        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        // End of variables declaration                   

    }

错误:

java.lang.NumberFormatException: For input string: "null"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:492)
        at java.lang.Integer.parseInt(Integer.java:527)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:145)
        at javax.mail.Service.connect(Service.java:297)
        at javax.mail.Service.connect(Service.java:156)
        at javax.mail.Service.connect(Service.java:105)
        at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:93)
        at javax.mail.Transport.send0(Transport.java:168)
        at javax.mail.Transport.send(Transport.java:98)
        at sendemail.SendMail.jButton1ActionPerformed(SendMail.java:177)
        at sendemail.SendMail.access$100(SendMail.java:25)
        at sendemail.SendMail$2.actionPerformed(SendMail.java:77)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
        at java.awt.Component.processMouseEvent(Component.java:6505)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3311)
        at java.awt.Component.processEvent(Component.java:6270)
        at java.awt.Container.processEvent(Container.java:2229)
        at java.awt.Component.dispatchEventImpl(Component.java:4861)
        at java.awt.Container.dispatchEventImpl(Container.java:2287)
        at java.awt.Component.dispatchEvent(Component.java:4687)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
        at java.awt.Container.dispatchEventImpl(Container.java:2273)
        at java.awt.Window.dispatchEventImpl(Window.java:2719)
        at java.awt.Component.dispatchEvent(Component.java:4687)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
        at java.awt.EventQueue.access$200(EventQueue.java:103)
        at java.awt.EventQueue$3.run(EventQueue.java:694)
        at java.awt.EventQueue$3.run(EventQueue.java:692)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
        at java.awt.EventQueue$4.run(EventQueue.java:708)
        at java.awt.EventQueue$4.run(EventQueue.java:706)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
    BUILD STOPPED (total time: 1 minute 2 seconds)

I am trying to send Email with SendMail.java while the sender's email and password is entered from Settings.java and the EmailClient.java is just for setting JFrames visibility to true or false on button click

2 个答案:

答案 0 :(得分:1)

  

java.lang.NumberFormatException: For input string: “null”

据我所知,当你尝试发送电子邮件时,唯一可以解析为数字的字段(并给你这样的例外)就是端口号。说完这个后,您可以在Settings课程的组合框中选择端口号:

port = jComboBox2.getSelectedItem().toString();

并在此SendEmail类的属性中设置此端口号:

String portnum = setfrm.getPort();
...
Properties properties = new Properties();
...
properties.put("mail.smtp.port", portnum.toString());

在您的代码中,您不会在组合框中添加任何对象。至少你没有显示那个部分,但你必须在某个地方这样做,因为否则你会得到一个NullPointerException

port = jComboBox2.getSelectedItem().toString();

但是port可能持有"null"字符串,因此以下调用会抛出异常:

Integer.parseInt("null"); // called somewhere within com.sun.mail.smtp.SMTPTransport.protocolConnect() method

答案 1 :(得分:1)

我认为dic19的回复以及对你问题的评论可以让你很好地了解问题所在。至于我,我会说以下(希望你觉得它很有用):

  // line numbers added by me for reference

  1:    MimeMessage message=new MimeMessage(session);
  2:    message.setFrom(new InternetAddress(user));
  3:    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(jTextField1.getText().toString()));
  4:    message.setSubject(subject);
  5:    message.setText(jTextArea1.getText());
  6:   Transport.send(message);

堆栈跟踪显示在Transport.send中抛出异常。因此,第3,4和5行中的任何参数都是罪魁祸首。

最简单的方法是将第3,4和5行中的参数临时分配到局部变量中:

        // validate your arguments first, always

        final String recipient = InternetAddress.parse(jTextField1.getText().toString());

        if( recipient == null || recipient.trim().length() == 0 )
        {
            throw sometypeofruntimeexception...
        }

        final String text = jTextArea1.getText());

        if( text == null )
        {
            throw sometypeofruntimeexception...
        }

        if( session == null ){ throw sometypeofruntimeexception... }
        if( user == null ){ throw sometypeofruntimeexception... }

        // now you are ready to build  your message

        MimeMessage message=new MimeMessage(session);
        message.setFrom(new InternetAddress(user));
        message.setRecipients(Message.RecipientType.TO, recipient);
        message.setSubject(subject);
        message.setText(test);
        Transport.send(message);

定期验证您的参数,然后,当错误发生时,您将更好地掌握问题所在。