ComboBox removeAllItems method does not remove

Hi,
I am using vaadin 7.1.x. I am using 2 comboboxes. I have added valuechangelistener on first one so that whenever I choose a product, corresponding data is generated in the second combobox. I am trying to clear the contents of second combobox whenever the first one changes.
I have used removeAllItems method and setValue(null) method. But iit does not clear the contents of second combobox.
This application is about loading directory names in a particular directory.
I am pasting the code here:

File file = new File(“C:\”);
String names = file.list();

    for(String name : names)
    {
        if (new File("C:\\" + name).isDirectory())
        {
             cbLine.addItem(name);
             
        }
    }
    
    cbLine.setNullSelectionAllowed(false);
    cbLine.setTextInputAllowed(false);
    cbLine.setImmediate(true);
    cbLine.addValueChangeListener(new ValueChangeListener()
    {
        @Override
        public void valueChange(ValueChangeEvent event)
        {
            
            cbLine1.removeAllItems();
          
        String product= (String) cbLine.getValue();
            File buildId = new File("C:\\" +product);
            String[] buildNames = buildId.list();

            for (String buildName:buildNames)
            {
            if(new File(buildId+"\\"+buildName).isDirectory())
            {        
                
            cbLine1.addItem(buildName);
            
                }
            } 
        }    
    });
    cbLine1.setNullSelectionAllowed(false);
    cbLine1.setTextInputAllowed(false);
    cbLine1.setImmediate(true);    

This is the trace when I use removeallItems method:
Feb 04, 2014 4:10:40 PM com.vaadin.server.DefaultErrorHandler doDefault
SEVERE:
com.vaadin.event.ListenerMethod$MethodException: Invocation of method valueChange in com.company.vapp.ui.setup.view.SetupViewImpl$1 failed.
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:528)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:167)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:969)
at com.vaadin.ui.AbstractField.fireValueChange(AbstractField.java:1126)
at com.vaadin.ui.AbstractField.setValue(AbstractField.java:542)
at com.vaadin.ui.AbstractSelect.setValue(AbstractSelect.java:702)
at com.vaadin.ui.ComboBox.changeVariables(ComboBox.java:664)
at com.vaadin.server.communication.ServerRpcHandler.changeVariables(ServerRpcHandler.java:396)
at com.vaadin.server.communication.ServerRpcHandler.handleBurst(ServerRpcHandler.java:221)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:111)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:91)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1371)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Caused by: com.vaadin.event.ListenerMethod$MethodException: Invocation of method valueChange in com.company.vapp.ui.setup.view.SetupViewImpl$2 failed.
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:528)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:167)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:969)
at com.vaadin.ui.AbstractField.fireValueChange(AbstractField.java:1126)
at com.vaadin.ui.AbstractField.setValue(AbstractField.java:542)
at com.vaadin.ui.AbstractSelect.setValue(AbstractSelect.java:702)
at com.vaadin.ui.AbstractSelect.setValue(AbstractSelect.java:671)
at com.vaadin.ui.AbstractSelect.removeAllItems(AbstractSelect.java:829)
at com.company.vapp.ui.setup.view.SetupViewImpl$1.valueChange(SetupViewImpl.java:156)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
… 31 more
Caused by: java.lang.NullPointerException
at com.company.vapp.ui.setup.view.SetupViewImpl$2.valueChange(SetupViewImpl.java:189)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
… 44 more

Line 156 is
cbLine1.removeAllItems();

​Can anyone please help?

Seems like cbLine1 is null at that point. Add a breakpoint

If cbLine1 is null, it works fine. When cbLine1 has some value, I am getting that exception. I debugged it. it’s failing at removeallItems() method. When I went through combobox class, there is no removeallItems method implemented. Our previous developers used BeanItemContainer to remove items from a combobox. But this is a simple application which does not even need a bean class.

I did a workaround for this. Incase anyone is looking for solution:

I have removed the listener on secondcombobox and added it back after first combobox contents are loaded.,

thanks. I have met the same problem and resolved it by using the solution you have mentioned.

the codes are pushed in
github