Jump to content

Combobox null pointer exception


lrdblk

Recommended Posts

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

Link to comment
Share on other sites

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());
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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