的我創建的JComboBoxArrayList的JComboBoxes
private static ArrayList<JComboBox> comboList = new ArrayList<>();
的ArrayList,然後加入JComboBox
每個實例的ArrayList
private void courseUnit() {
String[] units = {"6", "5", "4", "3", "2", "1"};
int x = 520, y = 30;
for (int i = 0; i < 10; i++) {
comboUnits = new JComboBox<>(units);
comboUnits.setBounds(x, y, 100, 25);
y += 30;
comboUnits.setToolTipText("Select course unit");
comboUnits.setVisible(true);
comboUnits.addActionListener(new PaneAction());
add(comboUnits);
comboList.add(comboUnits); //comboUnits added to ArrayList
}
}
我的問題是,我如何得到每個組合框的selectedItem屬性因爲我試過這個ArrayList
//this is supposed to get the selected item of the first ComboBox and assign to courseGrade[0]
public void actionPerformed(ActionEvent evt) {
String[] courseGrade = new String[10];
courseGrade[0] = (String)comboList.get(0).getSelectedItem();
and the pr圖沒有編譯。
郵政編譯錯誤 – mavroprovato 2014-11-24 12:39:28
java.lang.ExceptionInInitializerError 引起:java.lang.NullPointerException – 2014-11-24 12:42:01
這不是一個編譯錯誤,那是一個你的代碼拋出異常。你應該檢查堆棧跟蹤,它應該告訴你'NullPointerException'發生在哪裏。你忘了在那條線上初始化一些東西。 – mavroprovato 2014-11-24 12:47:46