-2

having problems with the code mentioned below. the eclipse error code is as follows ::

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at gui.PassengerGui$2.actionPerformed(PassengerGui.java:139)

Any recommendations would be very much appreciated? I have have been pulling my hair out for almost one hour trying to figure out what the problem is..

private JPanel contentPane;
private JFileChooser fileChooser;
private PassengerController controller;
private PassengerTabelPanel tabelPanel;
private JTextField nameField;
private JTextField cityField;
private JTable passengerTable;
private JRadioButton  standardRadio;
private JRadioButton businessRadio;
private ButtonGroup classGroup;
private PassengerFormListener formListener;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                PassengerGui frame = new PassengerGui();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public PassengerGui() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 584, 368);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenuItem importDataItem = new JMenuItem("Import Data");
    menuBar.add(importDataItem);

    JMenuItem exportDataItem = new JMenuItem("Export Data");
    menuBar.add(exportDataItem);

    fileChooser = new JFileChooser();
    fileChooser.addChoosableFileFilter(new PassengerFileFilter());
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    classGroup = new ButtonGroup();

    //set up class radio

            classGroup.add(standardRadio);
            classGroup.add(businessRadio);


    JLabel nameLabel = new JLabel("Name: ");
    nameLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
    nameLabel.setBounds(12, 11, 69, 31);
    contentPane.add(nameLabel);

    JLabel cityLabel = new JLabel("City: ");
    cityLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
    cityLabel.setBounds(12, 47, 69, 31);
    contentPane.add(cityLabel);

    JLabel lblNewLabel_2 = new JLabel("Class: ");
    lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblNewLabel_2.setBounds(12, 89, 69, 31);
    contentPane.add(lblNewLabel_2);

    nameField = new JTextField();
    nameField.setBounds(60, 18, 129, 20);
    contentPane.add(nameField);
    nameField.setColumns(10);

    cityField = new JTextField();
    cityField.setColumns(10);
    cityField.setBounds(60, 53, 129, 20);
    contentPane.add(cityField);

    JRadioButton businessRadio = new JRadioButton("Business");
    businessRadio.setBounds(58, 95, 109, 23);
    contentPane.add(businessRadio);

    JRadioButton standardRadio = new JRadioButton("Standard");
    standardRadio.setBounds(60, 127, 109, 23);
    contentPane.add(standardRadio);

    JButton btnSubmit = new JButton("Submit");
    btnSubmit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String name = nameField.getText();
            String city = cityField.getText();
            String passengerClass = classGroup.getSelection().getActionCommand();


            PassengerFormEvent ev = new PassengerFormEvent(this, name, passengerClass, city);
Paddy
  • 15
  • 4

2 Answers2

1

You aren't instantiating ButtonGroup variable classGroup. You are using dot (.) operator on a null reference.

Do this:

ButtonGroup classGroup = new ButtonGroup();

and Somewhere in your code after this instantiation, do this:

String passengerClass = classGroup.getSelection().getActionCommand(); 
coderpc
  • 2,685
  • 3
  • 31
  • 58
0

You are not instantiating classGroup. You need code such as:-

classGroup = new ButtonGroup();

before you can call methods on classGroup.

EDIT: In answer to the second problem, it's because the buttons you've added to the buttonGroup are also null. You need to create them before you add them.

Steve Smith
  • 2,008
  • 2
  • 14
  • 21