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