只要看下面的代码片段,
String[] choices = {"Apple", "Banana", "Custard"};
JComboBox<String> fruits = new JComboBox<String>(choices);
fruits.setSelectedItem("Custard");
它抛出空指针异常。见下文,
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.EventQueue.getCurrentEventImpl(Unknown Source)
at java.awt.EventQueue.getCurrentEvent(Unknown Source)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
setSelectedIndex()也会发生相同的问题。如果Java JRE 1.7有任何问题,请提出解决此问题的好的解决方法,或者建议我。
最佳答案
线程“ 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写道,如果我将该动作监听器添加到组合框中。它给
同样的错误。但是它在Java 1.6中可以正常工作
组合框通用类型。
不,我不是在谈论,您可以从此代码生成此异常
注释代码行(//)mainComboBox.setSelectedItem("Fruit“);
并取消注释//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);
}
});
}
}
关于swing - Java JRE 1.7 JCombobox <E> setSelectedItem(Object anObject)无法正常工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16812592/