在異常線程 「AWT-EventQueue的-0」 顯示java.lang.NullPointerException 在java.awt.EventQueue.getCurrentEventImpl(未知來源) 在java.awt.EventQueue.getCurrentEvent(未知來源) 在的javax .swing.JComboBox.fireActionEvent(未知來源) 在javax.swing.JComboBox.setSelectedItem(未知來源)
從
String[] choices = {"Apple", "Banana", "Custard"};
JComboBox<String> fruits = new JComboBox<String>(choices);
fruits.setSelectedItem("Custard");
可能僅在添加Action/ItemListener
setSelectedItem
前的情況下產生被稱爲(調試的是,什麼XxxListener
射擊),改變
String[] choices = {"Apple", "Banana", "Custard"};
JComboBox<String> fruits = new JComboBox<String>(choices);
fruits.setSelectedItem("Custard");
fruits.addAction/ItemListener(new Action/ItemListener)
和的Java6同樣的問題
@sanjay寫道:如果我將該actionlistener添加到組合框。它給出 相同的錯誤。但它在java 1.6中正常工作,沒有那個 Combobox泛型類型。
不,我說的不是,你可以從這個代碼生成此異常
註釋代碼行(//)mainComboBox.setSelectedItem("Fruit「);
and uncomment //mainComboBox.setSelectedItem("Shape「);
那麼這段代碼發射相同的異常,爲JComboBox中相當常見的問題,在的Java6同樣的問題(從JComboBox中定義消除仿製藥)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class FruitAndVedg extends JFrame implements ActionListener, ItemListener {
private static final long serialVersionUID = 4L;
private JComboBox<String> mainComboBox;
private JComboBox<String> subComboBox;
private ArrayList item;
private Hashtable<Object, Object> subItems = new Hashtable<>();
public FruitAndVedg() {
item = new ArrayList();
item.add("Select Item");
item.add("Fruit");
item.add("Vedg");
String[] items = {"Select Item", "Color", "Shape", "Fruit"};
mainComboBox = new JComboBox<>(items/*item.toArray()*/);
mainComboBox.setSelectedItem("Fruit");
mainComboBox.addActionListener(this);
mainComboBox.addItemListener(this);
//mainComboBox.setSelectedItem("Shape");
add(mainComboBox, BorderLayout.WEST);
subComboBox = new JComboBox<>();
subComboBox.setPrototypeDisplayValue("XXXXXXXXXX");
add(subComboBox, BorderLayout.CENTER);
String[] subItems1 = {"Select Fruit", "Apple", "Plum"};
subItems.put(items, subItems1);
String[] subItems2 = {"Select Vedg", "Carrot", "Peas"};
subItems.put(items, subItems2);
}
@Override
public void actionPerformed(ActionEvent ae) {
String item = (String) mainComboBox.getSelectedItem();
Object o = subItems.get(item);
if (o == null) {
subComboBox.setModel(new DefaultComboBoxModel());
} else {
subComboBox.setModel(new DefaultComboBoxModel((String[]) o));
}
}
@Override
public void itemStateChanged(ItemEvent ie) {
if (ie.getStateChange() == ItemEvent.SELECTED) {
if (ie.getSource() == mainComboBox) {
if (mainComboBox.getSelectedIndex() != 0) {
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new FruitAndVedg();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
我們不能根據3行代碼來判斷問題。有一些其他代碼導致了這個問題。發佈你的[SSCCE](http://sscce.org/)來證明問題。有機會,當你創建SSCCE時,你會發現你的問題。 – camickr