Question

I have a textfield where a user inputs a phone number, and a combo box where the user chooses their carriers SMS Gateway to send an email from the java application to a mobile phone. I am having no trouble grabbing a string from a textfield, but when I use,

String gateway = (String)comboBox_1.getSelectedItem();

or,

String gateway = comboBox_1.getSelectedItem().toString();

I get errors and the SMS won't send.

Here are the parts of my code that relate to the SMS messaging and the comboBox:

final String[] carriers = {"@txt.att.net", "@myboostmobile.com", "@messaging.sprintpcs.com", "@tmomail.net", "@vtext.com"};

...

JComboBox comboBox_1 = new JComboBox(carriers);
    comboBox_1.setSelectedIndex(-1);
    contentPane.add(comboBox_1);
    comboBox_1.setRenderer(new PromptComboBoxRenderer("Select Carrier Gateway"));
    ((JLabel)comboBox_1.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER); 

    textField_1 = new JTextField();
    contentPane.add(textField_1);
    textField_1.setColumns(10);
    textField_1.setHorizontalAlignment(JLabel.CENTER); 

...

    public class SMTPSend {

    public SMTPSend() {
    }

    public void msgSafe() {

      String number = textField_1.getText(); 
      String gateway = (String)comboBox_1.getSelectedItem();
      // alternatively tried .toString()
      String username = "email@gmail.com";
      String password = "password";
      String smtphost = "smtp.gmail.com"; 
      String compression = "subject"; 
      String from = "email@gmail.com";
      String to = number + gateway; // where number is the 10 digit phone number and gateway is @SMS_Gateway
      String body = "Hello World!";
      Transport myTransport = null;

try {
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

Session mailSession = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(compression);
msg.setText(body);
msg.setSentDate(new Date());

 myTransport = mailSession.getTransport("smtp");
  myTransport.connect(smtphost, username, password);
  msg.saveChanges();
  myTransport.sendMessage(msg, msg.getAllRecipients());
  myTransport.close();
 } 
catch (Exception e) {
    e.printStackTrace();
  }
}

If you need any more code from my application, I am more than happy to provide.

No correct solution

OTHER TIPS

My guess is that you don't have one of the carriers selected, and therefore a null value is being returned when you ask for the selected item. This demo seems to work - can you reproduce the problem with it?

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

public class ComboBoxDemo extends JPanel{

    public ComboBoxDemo(){

        final JComboBox cb = new JComboBox(new String[]{"@txt.att.net", "@myboostmobile.com", "@messaging.sprintpcs.com", "@tmomail.net", "@vtext.com"});
        cb.setSelectedIndex(-1);

        JButton button = new JButton("Print Selection");
        button.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                if(cb.getSelectedIndex() != -1)
                    System.out.println(cb.getSelectedItem());
                else
                    System.out.println("Not selected");
            }});


        add(cb);
        add(button);
    }

    public static void main(String[] args){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new ComboBoxDemo());
        frame.pack();
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top