2016-03-20 33 views
-1

我試圖根據員工的ID號在對話框中顯示組合框。當用戶選擇一個ID時,應用程序會顯示該員工的數據。對話框中的JComboBox

byIdItem.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 

    combo = new JComboBox(idNumbers); 
     String[] options = { "OK", "Cancel"}; 

     int selection = JOptionPane.showOptionDialog(null, combo, "Search", 
       JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, 
       options, options[0]); 
     if (selection > 0) { 
      System.out.println("selection is: " + options[selection]); 
      } 
     currentItem = ((JComboBox)(e.getSource())).getSelectedIndex(); 
     displayDetails(currentItem);} 
    }); 

因此,我只有null pointer exception。 在此先感謝!

堆棧跟蹤:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at javax.swing.DefaultComboBoxModel.<init>(DefaultComboBoxModel.java:53) 
    at javax.swing.JComboBox.<init>(JComboBox.java:175) 
    at SearchId.initComponents(SearchId.java:37) 
    at SearchId.<init>(SearchId.java:26) 
    at EmployeeMenu$6.actionPerformed(EmployeeMenu.java:255) 
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028) 
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) 
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) 
    at javax.swing.AbstractButton.doClick(AbstractButton.java:389) 
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:809) 
    at com.apple.laf.AquaMenuItemUI.doClick(AquaMenuItemUI.java:137) 
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:850) 
    at java.awt.Component.processMouseEvent(Component.java:6414) 
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275) 
    at java.awt.Component.processEvent(Component.java:6179) 
    at java.awt.Container.processEvent(Container.java:2084) 
    at java.awt.Component.dispatchEventImpl(Component.java:4776) 
    at java.awt.Container.dispatchEventImpl(Container.java:2142) 
    at java.awt.Component.dispatchEvent(Component.java:4604) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618) 
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279) 
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209) 
    at java.awt.Container.dispatchEventImpl(Container.java:2128) 
    at java.awt.Window.dispatchEventImpl(Window.java:2492) 
    at java.awt.Component.dispatchEvent(Component.java:4604) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717) 
    at java.awt.EventQueue.access$400(EventQueue.java:82) 
    at java.awt.EventQueue$2.run(EventQueue.java:676) 
    at java.awt.EventQueue$2.run(EventQueue.java:674) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97) 
    at java.awt.EventQueue$3.run(EventQueue.java:690) 
    at java.awt.EventQueue$3.run(EventQueue.java:688) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:687) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 
+0

什麼是堆棧跟蹤? –

+0

什麼是'idNumbers'? –

+0

如果idNumbers爲null,則獲得類似的堆棧跟蹤。另外currentItem =((JComboBox)(e.getSource()))。getSelectedIndex();不會包含組合'選定的價值指數。 e.getSource()是一個不同的對象。 – RubioRic

回答

1

我認爲你需要從變量combo檢索選定的索引。 當用戶選擇一個項目在組合的選擇項改變

因此,與去:selected_index = combo.getSelectedIndex();

相關問題