Jump to content

Combobox null pointer exception


Recommended Posts

Posted

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

Posted

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());
Posted (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 by Explv
  • Like 1
Posted

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...