$SourceException from Form.commit()

Hi Everybody,

I have a basic problem with Form. Tried to solve this, but could not figure it out. I need your help. Here is my problem:-

I have a Form with a data source attached to it. I am trying to update the data in the form. On click of update button, I am first calling Form.commit() and then updating the bean in database. It works fine, except in one case. There is a field of Integer type in bean class. If I don’t give any value to this field then throwing following error.

14:41:05,346 ERROR [de.mte.dialog.RefDataDialog]
(http– Failed to modify instance (null):$SourceException
at com.vaadin.ui.Form.commit( [vaadin-6.8.2.jar:6.8.2]

at de.mte.dialog.RefDataDialog.doOkAction( [classes:]

at de.mte.dialog.RefDataDialog.buttonClick( [classes:]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_31]

at sun.reflect.NativeMethodAccessorImpl.invoke( [rt.jar:1.6.0_31]

at sun.reflect.DelegatingMethodAccessorImpl.invoke( [rt.jar:1.6.0_31]

at java.lang.reflect.Method.invoke( [rt.jar:1.6.0_31]

at com.vaadin.event.ListenerMethod.receiveEvent( [vaadin-6.8.2.jar:6.8.2]

at com.vaadin.event.EventRouter.fireEvent( [vaadin-6.8.2.jar:6.8.2]

at com.vaadin.ui.AbstractComponent.fireEvent( [vaadin-6.8.2.jar:6.8.2]

at com.vaadin.ui.Button.fireClick( [vaadin-6.8.2.jar:6.8.2]

at com.vaadin.ui.Button.changeVariables( [vaadin-6.8.2.jar:6.8.2]

at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.changeVariables( [vaadin-6.8.2.jar:6.8.2]

at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariableBurst( [vaadin-6.8.2.jar:6.8.2]

at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables( [vaadin-6.8.2.jar:6.8.2]

at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest( [vaadin-6.8.2.jar:6.8.2]

at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest( [vaadin-6.8.2.jar:6.8.2]

at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service( [vaadin-6.8.2.jar:6.8.2]

at javax.servlet.http.HttpServlet.service( [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( [jbossweb-7.0.13.Final.jar:]

at org.apache.catalina.core.ApplicationFilterChain.doFilter( [jbossweb-7.0.13.Final.jar:]

at org.apache.catalina.core.StandardWrapperValve.invoke( [jbossweb-7.0.13.Final.jar:]

at org.apache.catalina.core.StandardContextValve.invoke( [jbossweb-7.0.13.Final.jar:]

at [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]

at [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]

at org.apache.catalina.core.StandardHostValve.invoke( [jbossweb-7.0.13.Final.jar:]

at org.apache.catalina.valves.ErrorReportValve.invoke( [jbossweb-7.0.13.Final.jar:]

at org.apache.catalina.core.StandardEngineValve.invoke( [jbossweb-7.0.13.Final.jar:]

at org.apache.catalina.connector.CoyoteAdapter.service( [jbossweb-7.0.13.Final.jar:]

at org.apache.coyote.http11.Http11Processor.process( [jbossweb-7.0.13.Final.jar:]

at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process( [jbossweb-7.0.13.Final.jar:]

at$ [jbossweb-7.0.13.Final.jar:]

at [rt.jar:1.6.0_31]

Caused by:$SourceException
at com.vaadin.ui.AbstractField.commit( [vaadin-6.8.2.jar:6.8.2]

at com.vaadin.ui.Form.commit( [vaadin-6.8.2.jar:6.8.2]

... 32 more

Caused by:$ConversionException: java.lang.reflect.InvocationTargetException
at [vaadin-6.8.2.jar:6.8.2]

at [vaadin-6.8.2.jar:6.8.2]

at com.vaadin.ui.AbstractField.commit( [vaadin-6.8.2.jar:6.8.2]

... 33 more

Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedConstructorAccessor121.newInstance(Unknown Source) [:1.6.0_31]

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance( [rt.jar:1.6.0_31]

at java.lang.reflect.Constructor.newInstance( [rt.jar:1.6.0_31]

at [vaadin-6.8.2.jar:6.8.2]

... 35 more

Caused by: java.lang.NumberFormatException: For input string: “”
at java.lang.NumberFormatException.forInputString( [rt.jar:1.6.0_31]

at java.lang.Integer.parseInt( [rt.jar:1.6.0_31]

at java.lang.Integer.<init>( [rt.jar:1.6.0_31]

... 39 more

Why is it taking empty string? Or how should I handle this if I have to use Form.commit() ? I don’t want to check the value
of each field before commit.

Related to this I have one more question. My update method can throw an exception. On exception I have to revert the changes in bean, that happened because of Form.commit() call?

Thanks in advance!

The commit is failing because you are trying to set the value from an empty TextField into a method that accepts Integers:

Caused by: java.lang.NumberFormatException: For input string: ""

The value is often null, but can also be an empty String, which causes the Exception. In Vaadin 7 you can fix this by calling TextField.setConverter(Integer.class), in Vaadin 6 you have to convert the value yourself before committing.

Thank you Thomas for your reply. I am using vaadin 6.7.5.

Do you have any idea on my second question.

“My update method can throw an exception. On exception I have to revert the changes in bean, that happened because of Form.commit() call?”

How can I revert the changes in bean in this case. Is there any method in Form which can do so? One way is to keep a back up before calling Form.commit, which I don’t want to do.

Unfortunately, there is no built-in backup of beans in vaadin; you need to fix failed commits yourself. Good news is that all your changes aren’t discarded; so the user can only fix the thing that failed and try again. But if you want to reset the form to the previously stored state you have to do it yourself, as I mentioned already.

Thank you Thomas. Thank you for the quick reply.