2011-08-24 24 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

相关问题