Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Combobox null pointer exception

Featured Replies

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

  • Author

just use a checkbox bro

 

LMAO i may actually do that.  Let's assume I want to learn something emote32342.png .  Gonna have to use combobox eventually

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

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

  • Author

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

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.