Results 1 to 3 of 3
  1. #1
    joelBeaton is offline Member
    Join Date
    Mar 2012
    Location
    England
    Posts
    13
    Rep Power
    0

    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.getListofOrganizations());
    
            try
            {
    
                this.orgSelectionComboBox.addItem("");                     // LINE 141
    
                for (Organization eachOrg : listOfOrgs)
                {
                    this.orgSelectionComboBox.addItem(eachOrg);
                }
            }
            catch (NullPointerException npe)
            {
                System.out.println("There was a Null Error while populating "
                        + "the org list. " + npe.getMessage());
                npe.printStackTrace();
    
            }
        }
    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
    java.lang.NullPointerException
    at guis.NewPersonGUI.populateOrgSelection(NewPersonGU I.java:141)
    at guis.NewPersonGUI.<init>(NewPersonGUI.java:55)
    at guis.RecruitmentGUI.addNewButtonActionPerformed(Re cruitmentGUI.java:1108)
    at guis.RecruitmentGUI.access$900(RecruitmentGUI.java :31)
    at guis.RecruitmentGUI$9.actionPerformed(RecruitmentG UI.java:509)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 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 Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    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.

    db
    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
    Location
    England
    Posts
    13
    Rep Power
    0

    Default Re: ComboBox - Adding Items Creating NPE

    Thanks.

    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, 12: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, 04:19 AM
  3. Adding \ removing items from Jlist
    By Desperado in forum AWT / Swing
    Replies: 10
    Last Post: 12-17-2009, 12: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
  •