2011-08-24 71 views
1

我無法刪除jcombobox中的第一個元素。我的代碼如下,無法從jcombobox中刪除第一個元素

JComboBox cBox= cBox= new JComboBox(); 
...  
while (cBox.getItemCount() > 0) 
    cBox.removeItemAt(0); 

對於測試運行,我有3個項目在cBox。當它到達removeItemAt(0)時,調試進入一些文件訪問代碼,這是絕對不相關的。這兩次會得到下面的例外。我試着直接得到相同的異常removeAllItems()。但是,removeItem(1)按照原樣工作,直到剩下1個元素。該例外不會崩潰的應用程序,我可以看到沒有項目後,所以它的工作一點。我究竟做錯了什麼。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at util.Gui$4.actionPerformed(Gui.java:111) 
at javax.swing.JComboBox.fireActionEvent(Unknown Source) 
at javax.swing.JComboBox.contentsChanged(Unknown Source) 
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source) 
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source) 
at javax.swing.DefaultComboBoxModel.removeElementAt(Unknown Source) 
at javax.swing.JComboBox.removeItemAt(Unknown Source) 
at util.Gui.prepareSubLists(Gui.java:164) 
at util.Gui$3.actionPerformed(Gui.java:97) 
at javax.swing.JComboBox.fireActionEvent(Unknown Source) 
at javax.swing.JComboBox.setSelectedItem(Unknown Source) 
at javax.swing.JComboBox.setSelectedIndex(Unknown Source) 
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source) 
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 
+1

如果你把你的實際代碼放在這裏,這將會很有用。這可能是mre說的那種情況,或者是任何其他導致NPE的問題! –

+0

問題出在您未顯示的代碼中,請參閱stacktrace的第一行:'at util.Gui $ 4.actionPerformed(Gui.java:111)' – kleopatra

+0

我必須添加一個空白項目,然後刪除其餘項目。它似乎無法從組合框項目中刪除所有項目。 – mechanicum

回答

1

是不是你的條件語句錯了?與if更換while,這樣

if(cBox.getItemCount() > 0){ 
    cBox.removeItemAt(0); 
} 

下面是一個SSCCE

public final class JComboBoxDemo { 
    public static void main(String[] args){ 
     SwingUtilities.invokeLater(new Runnable(){ 
      @Override 
      public void run() { 
       createAndShowGUI();    
      } 
     }); 
    } 

    public static void createAndShowGUI(){ 
     final JFrame frame = new JFrame("JComboBox Demo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(new FlowLayout()); 
     frame.getContentPane().add(JComboPane.newInstance()); 
     frame.setSize(new Dimension(250, 100)); // for demonstration purposes only 
     //frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    private static final class JComboPane extends JPanel{ 
     private JComboPane(){ 
      super(); 
      setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 
      JCenteredComboBox comboBox = JCenteredComboBox.newInstance(); 
      JCenteredButton button = JCenteredButton.newInstance(comboBox); 
      add(comboBox); 
      add(button); 
     } 

     public static final JComboPane newInstance(){ 
      return new JComboPane(); 
     } 

     private static final class JCenteredComboBox extends JComboBox{ 
      private JCenteredComboBox(){ 
       super(new String[]{"Item 1", "Item 2", "Item 3"}); 
       setAlignmentX(Component.CENTER_ALIGNMENT); 
      } 

      public static final JCenteredComboBox newInstance(){ 
       return new JCenteredComboBox(); 
      } 
     } 

     private static final class JCenteredButton extends JButton{ 
      private JCenteredButton(final JComboBox comboBox){ 
       super("Remove First Item"); 
       setAlignmentX(Component.CENTER_ALIGNMENT); 
       addActionListener(new ActionListener(){ 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         if(comboBox.getItemCount() > 0){ 
          comboBox.removeItemAt(0); // your logic 
         } 
        } 
       }); 
      } 

      public static final JCenteredButton newInstance(final JComboBox comboBox){ 
       return new JCenteredButton(comboBox); 
      } 
     } 
    } 
} 

enter image description here

當你運行它,按JButton將刪除JComboBox的第一個項目。您可以繼續按下直到它爲空。

+1

在while中看不到任何錯誤 - 邏輯與你的if相同,只是重複。我錯過了什麼? – kleopatra

+0

@kleopatra,你說得對。它看起來像OP基本上執行「全部刪除」。但這仍然意味着邏輯錯誤。問題肯定在其他地方。如果OP沒有更新我們,我會刪除這個答案。 – mre

+0

現在對我有好處+1 – mKorbel

0

可能會發生此異常,因爲當組合項目被刪除時觸發事件,並且在此事件處理方法中仍然引用組合框項目。例如,當您在代碼中刪除某處(actionPeformed()除外)中的最後一個項目時,combo.removeItemAt(0)或removeAllItems()組合框中的最後一個項目仍然會被觸發/執行。但通常actionPerformed()方法包含對用戶操作作出反應的代碼(用戶在組合框某處單擊)。因此,當最後一個項目被刪除時,組合框中沒有更多項目,並且對actionPerformed()中的項目或索引的任何引用都將導致異常。

對此的解決方案是將代碼從actionPerformed移動到例如mouseClicked()或其他事件處理程序,具體取決於你想要做什麼。

+0

不,這不會發生在行爲良好的代碼中;-)爲了使其行爲良好,只需始終檢查一個您想要做的事情是否仍在模型中 - 在_any_方法中(包括actionPerformed) – kleopatra

相關問題