companyItem.addItemProperty(“streetAddress”, new NestedMethodProperty(company, “address.streetAddress”)); //with small a
companyItem.addItemProperty(“city”, new NestedMethodProperty(company, “Address.City.city”)); //with capital A
My POJO-structure is like that:
Company—>Address---->streetAddress/address/zip…
Company—>Address---->City—>city
I can see the field in the form, but there is error like that:
com.vaadin.data.util.MethodProperty$MethodException
at com.vaadin.data.util.NestedMethodProperty.invokeSetMethod(NestedMethodProperty.java:234)
at com.vaadin.data.util.NestedMethodProperty.setValue(NestedMethodProperty.java:214)
at com.vaadin.ui.AbstractField.commit(AbstractField.java:241)
at com.vaadin.ui.Form.commit(Form.java:339)
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:500)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:164)
at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1193)
at com.vaadin.ui.Button.fireClick(Button.java:539)
at com.vaadin.ui.Button.changeVariables(Button.java:206)
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:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.NullPointerException
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.data.util.NestedMethodProperty.invokeSetMethod(NestedMethodProperty.java:230)
… 34 more
Q1: So could someone tell me what is wrong and why?
First read
this post and my reply to it. Briefly, BeanItem, MethodProperty and NestedMethodProperty all rely on standard Java Beans naming and other Java Beans requirements.
You should use “new NestedMethodProperty(company, “address.city.city”)” - and the field names should naturally be “address”, “city” and “city”, conforming to standard Java naming conventions. Each property needs to have a correctly named setter and getter with correct parameter types (e.g. getAddress(), setCity(City), …).
I tired that already because Jens jansson assumed that to be the problem, but It didn’t help.
Company company = new Company();
BeanItem<Company> companyItem = new BeanItem<Company>(company);
companyItem.addItemProperty("companyName", new ObjectProperty(null, String.class));
companyItem.addItemProperty("streetAddress", new NestedMethodProperty(company, "address.streetAddress"));
companyItem.addItemProperty("city", new NestedMethodProperty(company, "address.city.city"));
List<String> visibleProperties = new ArrayList<String>();
visibleProperties.add("companyName");
//visibleProperties.add("address");
visibleProperties.add("streetAddress");
visibleProperties.add("city");
basicInfoForm.setItemDataSource(companyItem,
visibleProperties);
[b]
Company:
[/b]
private Address address;
public Address getAddress() {
return this.address;
}
public void setAddress(Address address) {
this.address = address;
}
City has basic getters and setters as well and I have no idea what is wrong with that If you have, let me pls know! Or if you have some other solution to handle nested pojos, that would be nice as well.
The other possibility: your address or city might be null.
As stated in the javadoc, the current version of NestedMethodProperty does not support intermediate properties being null. There has been discussion about the option of returning null as the nested property value in that case - I created
#7229 for this.
Note that in any case, null intermediate properties would not work when setting values.
I just think that BeanItem with overriding attachField is just not possible? What is the easiest way to implement a form with textfield and comboboxes? BTW if you are using nested forms, why the inner form is always little bit inner than the "mother"form (alignment is not LEFT for both of them). I just tried to make two forms using cutsomField add-on but It wasn’t piece of cake to me.
Suomeksi: Jos haluan siis käyttää BeanItemia (en muista tiedä) niin miten saan kahden kentän väliin esim kannasta haetun comboboxin. Onko pakko ylikirjoittaa juurikin attachField, mutta kutsutaanko sitä metodia jos käytetään BeanItemia?