The collection passed to BeanItemContainer must not be null or empty

I write the follwing


	public BeanItemContainer<MyBean> getListaMyBean() {
		List<Ciudad> lista = null;
		try {
			lista = this.getControl().getServiceControl()
					.getListMyBean(this.getBeanOne());
		} catch (ExcepcionDao e) {

			e.printStackTrace();
		}
		BeanItemContainer<MyBean> cr = new BeanItemContainer<MyBean>(lista);
		
		return cr;
	}

then if list is null because no get value BeanItemContainer =null

but shows

23:34:00,455 [http-80-4]
DEBUG [http-80-4]

  • {rset-100020} ResultSet
    com.vaadin.event.ListenerMethod$MethodException
    Cause: java.lang.IllegalArgumentException: The collection passed to BeanItemContainer must not be null or empty
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:507)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
    at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1154)
    at com.vaadin.ui.TabSheet.fireSelectedTabChange(TabSheet.java:772)
    at com.vaadin.ui.TabSheet.setSelectedTab(TabSheet.java:498)
    at com.vaadin.ui.TabSheet.changeVariables(TabSheet.java:564)

because it takes the null value and displays this message

Seems to work as expected? You should have a null check


BeanItemContainer<MyBean> cr;
if(lista != null)
    cr = new BeanItemContainer<MyBean>(lista);
else
    cr = new BeanItemContainer<MyBean>(MyBean.class);

As an addition, in 6.5 there is coming a new contstructor to BeanItemContainer which takes in both the list and class. With that you don’t have to do the null check that kim presented anymore. Hurray \o/

for example


public BeanItemContainer<Unidad> getListaUnidad() {
		[b]
List<Unidad> lista = null;
[/b]
		[b]
BeanItemContainer<Unidad> cu = new BeanItemContainer<Unidad>(
				Unidad.class);
[/b]
		try {
			lista = this.getControladorInventario().getServicioUnidad()
					.getListaUnidad(this.getCiudadSeleccionada());
		} catch (ExcepcionDao e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				return cu;

	}

now ,if the lista is null ,because BeanItemContainer is not null , because???

my friend uses your code, but if the list is no message displays

The collection passed to BeanItemContainer must not be null or empty

now , maybe not the best way but it works I write the following

[code]


for (MyBean bean : lista) {
cr.addItem(bean);
}
return cr;

[/code][i]

[/i]

Kim already give you the code needed to do it right. You should check if the list is null or not, and choose the constructor according to that. Here’s the code you need, once again:

public BeanItemContainer<Unidad> getListaUnidad() {
        List<Unidad> lista = null;
        try {
            lista = this.getControladorInventario().getServicioUnidad()
                    .getListaUnidad(this.getCiudadSeleccionada());
        } catch (ExcepcionDao e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BeanItemContainer<Unidad> cu;
        if(lista != null && !lista.isEmpty()){
                cu = new BeanItemContainer<Unidad>(lista);
        } else {
                cu = new BeanItemContainer<Unidad>(Unidad.class);
        }
        return cu;
}

OK :bashful::bashful::bashful: