Java JRE 1.7 JCombobox <e> setSelectedItem(Object anObject)无法正常工作?</e>

时间:2013-05-29 11:23:54

标签: swing java

请参阅以下代码段,

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

2 个答案:

答案 0 :(得分:3)

  

线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException        at java.awt.EventQueue.getCurrentEventImpl(Unknown Source)        at java.awt.EventQueue.getCurrentEvent(Unknown Source)        在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泛型类型。

  • 不是,我不是在谈论,你可以从这段代码生成这个例外

    1. 评论代码行(//)mainComboBox.setSelectedItem(“Fruit”);

    2. 并取消注释//mainComboBox.setSelectedItem("Shape“);

然后这个代码触发相同的异常,这是JComboBox的常见问题,Java6中的同样问题(通过从JComboBox定义中删除Generics)

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);
            }
        });
    }
}

答案 1 :(得分: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);