1

只需看下面的代码片段,

    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 有任何问题,请建议解决此问题的好方法或建议我。

4

2 回答 2

3

java.awt.EventQueue.getCurrentEventImpl(Unknown Source) at java.awt.EventQueue.getCurrentEvent(Unknown Source) at java.swing.JComboBox.fireActionEvent(Unknown Source) 线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException ) 在 javax.swing.JComboBox.setSelectedItem(Unknown Source)

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 写道,如果我将该动作侦听器添加到组合框中。它给出了同样的错误。但它在没有 Combobox 泛型类型的 java 1.6 中正常工作。

  • 不,我不是在说,你可以从这段代码中生成这个异常

    1. 注释代码行 (//)mainComboBox.setSelectedItem("Fruit");

    2. 并取消注释 //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);
            }
        });
    }
}
于 2013-05-29T11:30:27.847 回答
0

这是可能解决 NPE 的解决方法

String[] items = {"Select Item", "Color", "Shape", "Fruit"};
mainComboBox = new JComboBox<>(items/*item.toArray()*/);

SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
        mainComboBox.setSelectedItem("Fruit");
    }
});

mainComboBox.addActionListener(this);
mainComboBox.addItemListener(this);
于 2017-02-16T13:03:11.347 回答