Got an Excpetion while adding Items to a Combobox

Hello everyone,

when i get a Combobox from a Form and i want to add more Items to the Combobox, i always get something like this:


com.vaadin.event.ListenerMethod$MethodException
Cause: java.lang.NullPointerException
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:164)
	at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1193)
	at com.vaadin.ui.AbstractField.fireValueChange(AbstractField.java:891)
	at com.vaadin.ui.AbstractField.setValue(AbstractField.java:529)
	at com.vaadin.ui.AbstractSelect.setValue(AbstractSelect.java:666)
	at com.vaadin.ui.Select.changeVariables(Select.java:658)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariableBurst(AbstractCommunicationManager.java:1299)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1219)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:735)
	at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:296)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:501)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
	at com.vaadin.data.util.IndexedContainer$IndexedContainerItem.<init>(IndexedContainer.java:673)
	at com.vaadin.data.util.IndexedContainer$IndexedContainerItem.<init>(IndexedContainer.java:655)
	at com.vaadin.data.util.IndexedContainer.addItem(IndexedContainer.java:261)
	at com.vaadin.ui.AbstractSelect.addItem(AbstractSelect.java:826)
	at de.rhenus.sef_test.tabs.CommonInfoForm$1.valueChange(CommonInfoForm.java:81)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:490)
	... 24 more

everyone seen this bevor when doing:


					for (String current : stringcollection) {
						myCombo.addItem(current);
					}

crazy … isn’t it !!!

thanks for help

steffan

Your “stringcollection” contains NULL-values. The indexed container (the container behind the combobox) then throws a NullPointerException.
Try this:


for (String current : stringcollection) {
    if (current != null) myCombo.addItem(current);
}

But the better way is to not add null values to your collection :wink:

thanks that does the trick …

A notice to the developers:

An IllegalArgumentException would fit better in this place instead of a NullPointerException :smug:

IndexedContainer:684


            if (itemId == null) {
                throw new NullPointerException(); // throw new IllegalArgumentException("itemId may not be null!");
            }

I certainly agree, and the related ticket (
#2791
) has been sitting too long in the backlog.