2016-03-20 14 views
-1

Ich versuche Combobox im Dialog basierend auf ID-Nummern von Mitarbeitern anzuzeigen. Wenn der Benutzer eine ID auswählt, zeigt die Anwendung Daten dieser Mitarbeiter an.JComboBox in Dialog

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

Als Ergebnis habe ich nur null pointer exception. Vielen Dank im Voraus!

Stack-Trace:

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

Was der Stacktrace ist? –

+0

Was ist "idNumbers"? –

+0

Wenn idNumbers gleich null ist, erhalten Sie eine ähnliche Stack-Trace. Auch currentItem = ((JComboBox) (e.getSource())). GetSelectedIndex(); wird nicht den ausgewählten Wertindex der Combo enthalten. e.getSource() ist ein anderes Objekt. – RubioRic

Antwort

1

Ich glaube, Sie müssen den ausgewählten Index aus der Variablen combo abzurufen. Wenn der Benutzer ein Element auswählt, das ausgewählte Element Ihrer Combo ändert

So geht mit: selected_index = combo.getSelectedIndex();