Results 1 to 3 of 3
  1. #1
    joelBeaton is offline Member
    Join Date
    Mar 2012
    Rep Power

    Default ComboBox - Adding Items Creating NPE

    I am trying to populate a JComboBox in a GUI (using swing). When I execute this, for some reason I keep getting a Null Point Exception error and I cannot figure out why. Specifically because in another application the exact same commands work just fine.

    The commands I am using are:

    Java Code:
         * Method to populate the list of organisations able to be selected. 
        private void populateOrgSelection()
            ArrayList<Organization> listOfOrgs = new ArrayList<Organization>(
                this.orgSelectionComboBox.addItem("");                     // LINE 141
                for (Organization eachOrg : listOfOrgs)
            catch (NullPointerException npe)
                System.out.println("There was a Null Error while populating "
                        + "the org list. " + npe.getMessage());
    Organization is an enum that is available to the class that contains this method.

    If I delete this.orgSelectionComboBox.addItem("") I still get an NPE but on the forEach addItem() command line instead.

    I put a System.out.println(eachOrg) statement in the forEach statement I get a printed listed of each of the Organizations. So the ArrayList collection I am using is not null.

    The stack trace for this is:

    There was a Null Error while populating the org list. null
    at guis.NewPersonGUI.populateOrgSelection(NewPersonGU
    at guis.NewPersonGUI.<init>(
    at guis.RecruitmentGUI.addNewButtonActionPerformed(Re
    at guis.RecruitmentGUI.access$900( :31)
    at guis.RecruitmentGUI$9.actionPerformed(RecruitmentG
    at javax.swing.AbstractButton.fireActionPerformed(Abs
    at javax.swing.AbstractButton$Handler.actionPerformed (
    at javax.swing.DefaultButtonModel.fireActionPerformed (
    at javax.swing.DefaultButtonModel.setPressed(DefaultB
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen
    at java.awt.Component.processEvent( 2)
    at java.awt.Container.processEvent( 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent( 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent(
    at java.awt.LightweightDispatcher.processMouseEvent(C
    at java.awt.LightweightDispatcher.dispatchEvent(Conta
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl( )
    at java.awt.Component.dispatchEvent( 60)

    I am totally stymied as to why I am getting this error - Any ideas from anyone?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: ComboBox - Adding Items Creating NPE

    Did you initialize orgSelectionComboBox?

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    joelBeaton is offline Member
    Join Date
    Mar 2012
    Rep Power

    Default Re: ComboBox - Adding Items Creating NPE


    Simplicity at its best - my initializeComponents() method had been accidentally deleted and I failed to see that. Thanks for pointing out the obvious! Needed sometimes....

Similar Threads

  1. Replies: 1
    Last Post: 02-03-2012, 01:49 PM
  2. Adding items from one ArryList to another
    By Java-Guy in forum New To Java
    Replies: 4
    Last Post: 01-17-2011, 05:19 AM
  3. Adding \ removing items from Jlist
    By Desperado in forum AWT / Swing
    Replies: 10
    Last Post: 12-17-2009, 01:48 PM
  4. [SOLVED] Adding ComboBox
    By impact in forum New To Java
    Replies: 2
    Last Post: 05-02-2008, 08:43 PM
  5. Adding items to a jComboBox
    By tronovan in forum New To Java
    Replies: 0
    Last Post: 08-08-2007, 08:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts