2013-05-29 49 views
1

就看到下面的代碼片段,Java JRE 1.7 JCombobox <E> setSelectedItem(Object anObject)工作不正常?

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有任何問題。

+0

我們不能根據3行代碼來判斷問題。有一些其他代碼導致了這個問題。發佈你的[SSCCE](http://sscce.org/)來證明問題。有機會,當你創建SSCCE時,你會發現你的問題。 – camickr

回答

3

在異常線程 「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/ItemListenersetSelectedItem前的情況下產生被稱爲(調試的是,什麼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. 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); 
      } 
     }); 
    } 
} 
+0

如果我將該actionlistener添加到組合框。它給出了同樣的錯誤。但它在java 1.6中正常工作,沒有那個Combobox泛型類型。 – sanjay

+0

是啊mKorbel ..它工作正常..但請問你能解釋一下這個問題嗎? – sanjay

+0

Listener將事件觸發到此時不可顯示的GUI,然後有兩個選項,在將任何Listener添加到JComboBox之前或在調用JFrame.setVisible(true)之後的setSelectedItem,但Listener可以觸發事件,取決於你想做什麼 – mKorbel

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