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:
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?
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
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....