OptionGroup, ContainerDataSource and NullSelection

Hi !

I am trying to represent a list of selectable beans in an OptionGroup.
I also need to let the user select nothing when appropriate (so to have an option not bound to a bean)
As far as I understand, this is what the setNullSelectionItemId is designed for.

So this is how I proceeded :


... fill container with items containing a "name" property (BeanItemContainer) ...
OptionGroup group = new OptionGroup();
group.setNullSelectionAllowed(true);
String nullSelectionString = new String("Null selection");
group.addItem(nullSelectionString);
group.setNullSelectionItemId(nullSelectionString);
group.setItemCaptionPropertyId("name");
group.setContainerDataSource(container);

This code makes the application crash during the rendering with the following error :


java.lang.NullPointerException
        at com.vaadin.data.util.BeanItemContainer.getContainerProperty(BeanItemContainer.java:482)
        at com.vaadin.ui.AbstractSelect.getContainerProperty(AbstractSelect.java:745)
        at com.vaadin.ui.AbstractSelect.getItemCaption(AbstractSelect.java:1092)
        at com.vaadin.ui.AbstractSelect.paintItem(AbstractSelect.java:376)
        at com.vaadin.ui.OptionGroup.paintItem(OptionGroup.java:69)
        at com.vaadin.ui.AbstractSelect.paintContent(AbstractSelect.java:334)
        at com.vaadin.ui.OptionGroup.paintContent(OptionGroup.java:63)
        at com.vaadin.ui.AbstractComponent.paint(AbstractComponent.java:754)
        at com.vaadin.ui.AbstractOrderedLayout.paintContent(AbstractOrderedLayout.java:171)
        at com.vaadin.ui.AbstractComponent.paint(AbstractComponent.java:754)
        at com.vaadin.ui.Panel.paintContent(Panel.java:247)
        at com.vaadin.ui.Window.paintContent(Window.java:621)
        at com.vaadin.ui.AbstractComponent.paint(AbstractComponent.java:754)
        at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.writeUidlResponce(AbstractCommunicationManager.java:1019)
        at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.paintAfterVariableChanges(AbstractCommunicationManager.java:906)
        at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:829)
        at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:291)
        at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:481)
....

The same code works when adding the items using “group.addItem” instead of using setContainerDataSource.
Using setContainerDataSource also works well I remove the null selection related code.

Am I doing something wrong ?

As I get the same behaviour when replacing the OptionGroup by a NativeSelect, I guess I am doing something wrong …

Hi,

Can you point which line of your code throws the exception? It’s weird you have

6group.addItem(nullSelectionString);

9group.setContainerDataSource(container);

Shouldn’t you add the item into container instance and set container to optiongroup’s datasource? I haven’t tried your code, but in C# combobox what you did is wrong.

Br,
Xuan

Hi,

The exception is not occuring in my code, but in vaadin’s code (probably when it’s trying to render)

I want to be able to use a standard datasource, with the possibility to have a “no selection” item displayed.
Is that possible ?

When you add an item directly to the ComboBox, it simply forwards the call to its container, which at that point is an implicitly created IndexedContainer. Setting the container after that simply discards the old automatically created container.

I guess what you want to do can be achieved with setInputPrompt(String). If not, you need to add the extra item to your real container.

Thank you for your reply

Out of curiosity, what is the setNullSelectionItemId function’s purpose ?

It looks like I did not read your original post carefully enough, or I mixed it up with another post.

Several selection or other input components, such as ComboBox and TextField, support an input prompt, but an OptionGroup does not.

With an OptionGroup, you therefore do need setNullSelectionItemId(). To make it work, the item representing null needs to be in the container used by the OptionGroup - you cannot add it to the OptionGroup before setting the container, so you should create a dummy item when setting up your BeanItemContainer or add one to it before setting it as the container for the OptionGroup.

Thank you for your reply

Unfortunately I still cannot get it to work.

Should I create a custom container with an item Id of value null (I tried, it crashed vaadin) ? Or insert a normal Id in my container that is not bound to real data (I tried and although the item was shown, selecting it did not result in an null value when calling “getValue”) ?