与下面提到的代码有问题。Eclipse错误代码如下::
线程" awt-esventqueue-0" java.lang.nullpointerexception中的例外 在gui.passengergui $2。
任何建议将不胜感激吗?我已经把头发拔出了近一个小时,试图弄清楚问题是什么。
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);
您没有实例化ButtonGroup
变量classGroup
。您在null引用中使用dot(。(操作员。
这样做:
ButtonGroup classGroup = new ButtonGroup();
以及在此实例化之后的代码中的某个地方,执行此操作:
String passengerClass = classGroup.getSelection().getActionCommand();
您没有实例化 classGroup
。您需要诸如: -
classGroup = new ButtonGroup();
在您可以在classGroup
上调用方法。
编辑:在回答第二个问题时,这是因为您添加到对接的按钮也为无效。您需要在添加它们之前创建它们。