MasterDetailEditor throws NullPointerException when adding new Entity

I am sorry for pestering the forum but I ran into another problem. As you might know I had trouble with JPAContainer’s FieldFactory recently and was able to find out what was going wrong (
see my post here

Well not quite, it turns out when I pass a new Entity containing a @OneToMany field a NullPointerException is throw by the MasterDetailEditor.

Is this a bug or is there something else I do not see?

Thanks for your help.



Caused by: java.lang.NullPointerException
	at com.vaadin.addon.jpacontainer.fieldfactory.MasterDetailEditor.<init>(
	at com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory.createOneToManyField(
	at com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory.createJPAContainerBackedField(
	at com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory.createField(
	at com.vaadin.ui.Form.setItemDataSource(
	at com.vaadin.ui.Form.setItemDataSource(
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at com.vaadin.event.ListenerMethod.receiveEvent(
	... 36 more

This is how I create the entity:

    final EntityItem newUserGroupItem = userGroups.createEntityItem(new UserGroup());
    UserGroupEditor userGroupEditor = new UserGroupEditor(newUserGroupItem);

This is my editor:

        public UserGroupEditor(Item userGroupItem) {
                this.userGroupItem = userGroupItem;

                editorForm = new Form();
                // Use a JPAContainer field factory
                //  - no configuration is needed here according to the Book of Vaadin
                final FieldFactory fieldFactory = new FieldFactory();

                saveButton = new Button("Save", this);
                cancelButton = new Button("Cancel", this);

                setCaption("UserGroup Editor");

I have the same problem. It´s occur because de method createEntity doesn´t create id for the item. The masterdetaileditor use the itemid to retrieve the item; as has null value the error occur. Look below (red line)
I have a one-to-many relationship with two tables and I don´t know yet how resolve it.

public MasterDetailEditor(FieldFactory fieldFactory,
EntityContainer<?> containerForProperty, Object itemId,
Object propertyId, Component uiContext) {
this.fieldFactory = fieldFactory;
this.containerForProperty = containerForProperty;
this.itemId = itemId;
this.propertyId = propertyId;

[/size] masterEntity = containerForProperty.getItem(itemId).getEntity();

boolean writeThrough = true;
if (uiContext instanceof Form) {
Form f = (Form) uiContext;
writeThrough = f.isWriteThrough();



Thanks for your feedback. You are right. The missing itemId causes the exception. But I also do not know how to solve this. At least not yet. :slight_smile:

I’ve just run into the same problem half an hour ago. I solved it with a workaround: I only display the @OneToMany relations in “edit mode” and that way the user have to save the new entity with basic data first, then reopen the window in “edit mode”.

I am thinking about adding another button next to “Save”: something like “Save and keep editing” which will save the entity and immediatelly reopen it in edit mode. That way I can save some clicks for the user at least.

Although a bugfix would be great for this.

So we all think this is a bug, right? If so, shall I create a ticket?

Is there any update on this? I’ve been getting the same issue as well. Adding a new entity to the container is not always possible as constraints could be violated.

Sandor, your workaround seems like the best option at the moment.

I ended up changing the MasterDetailEditor to have the EntityItem as a constructor argument instead of the itemId. This solved my problem. I’ve attached my class as ‘PlasmaDetailEditor’. You’ll also have to extend the FieldFactory slightly but that shouldn’t be too difficult as the target ‘Item’ is already present in the createField method.

While I was in the code I also changed the table so the page length goes between 0 → 5 (but the MAX_PAGE_LENGTH) is a final int at the top of the class. This way the table takes up as little space as possible when it is empty to conserve real estate. If you want a constant size, feel free to comment that out :stuck_out_tongue:

Cheers (7.86 KB)

This thread is three years old but the problem is current!
How is it possible that this thing is not fixed?
Is there another way to do the same thing?
Anyway I have seen Erich solution but I need also to subclass FieldFactory. I would like to understand better Sandor solution: how can I “disable” @onetomany relations? How can I save only basic data?

Thanks for any help, I am stalled.