lrdblk Posted January 18, 2017 Share Posted January 18, 2017 So I made a Combobox and added stuff to it like so... cmbBank = new JComboBox<>(); cmbBank.setModel(new DefaultComboBoxModel<>(new String[] { "YES", "NO" })); I want to convert the choice to a boolean. Start button code.. private void button1ActionPerformed(ActionEvent e) { script.setShouldBank(getSelection(cmbBank.getSelectedItem())); script.setShouldStart(true); //start script this.setVisible(false); } private boolean getSelection(Object o){ if(o.toString().equalsIgnoreCase("yes")){ return true; } return false; } Error that I'm getting ERROR][01/18 03:29:45 PM]: Uncaught exception! java.lang.NullPointerException at GuiMain.cmbBankActionPerformed(GuiMain.java:36) at GuiMain.lambda$initComponents$0(GuiMain.java:65) 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.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(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) What am I doing wrong? Or is there a better way to do this Quote Link to comment Share on other sites More sharing options...
Chris Posted January 18, 2017 Share Posted January 18, 2017 just use a checkbox bro Quote Link to comment Share on other sites More sharing options...
lrdblk Posted January 18, 2017 Author Share Posted January 18, 2017 just use a checkbox bro LMAO i may actually do that. Let's assume I want to learn something . Gonna have to use combobox eventually Quote Link to comment Share on other sites More sharing options...
Hayase Posted January 18, 2017 Share Posted January 18, 2017 Since you are using a combobox and your options are stored in an array of strings why not just handle them based on the index of the string? We know that at index 0 in your box, answer is "Yes" setShouldBank = cmbBank.getSelectedIndex() == 0;If the selected index is 0, setShouldBank will be true. If you absolutely need the name of the value use this: String selectedIndex = cmbBank.getItemAt(cmbBank.getSelectedIndex()); Quote Link to comment Share on other sites More sharing options...
Explv Posted January 18, 2017 Share Posted January 18, 2017 (edited) So I made a Combobox and added stuff to it like so... cmbBank = new JComboBox<>(); cmbBank.setModel(new DefaultComboBoxModel<>(new String[] { "YES", "NO" })); I want to convert the choice to a boolean. Start button code.. private void button1ActionPerformed(ActionEvent e) { script.setShouldBank(getSelection(cmbBank.getSelectedItem())); script.setShouldStart(true); //start script this.setVisible(false); } private boolean getSelection(Object o){ if(o.toString().equalsIgnoreCase("yes")){ return true; } return false; } What am I doing wrong? Or is there a better way to do this In this case a JCheckBox would make more sense, however, if you want to use a JComboBox, this works: JComboBox<String> optionSelector = new JComboBox<>(new String[]{ "Yes", "No" }); To check if "Yes" is selected: boolean yesSelected = optionSelector.getSelectedItem().toString().equals("Yes"); Edited January 18, 2017 by Explv 1 Quote Link to comment Share on other sites More sharing options...
lrdblk Posted January 18, 2017 Author Share Posted January 18, 2017 Since you are using a combobox and your options are stored in an array of strings why not just handle them based on the index of the string? We know that at index 0 in your box, answer is "Yes" setShouldBank = cmbBank.getSelectedIndex() == 0;If the selected index is 0, setShouldBank will be true.If you absolutely need the name of the value use this: String selectedIndex = cmbBank.getItemAt(cmbBank.getSelectedIndex()); In this case a JCheckBox would make more sense, however, if you want to use a JComboBox, this works: JComboBox<String> optionSelector = new JComboBox<>(new String[]{ "Yes", "No" }); To check if "Yes" is selected: boolean yesSelected = optionSelector.getSelectedItem().toString().equals("Yes"); got it working, the issue was completly unrelated. fucked up my GUI constructor which was causing issues. for the record, both your solutions worked for me though. thanks for your help Quote Link to comment Share on other sites More sharing options...