Avoid JS exception and favor Java precondition assertion to validate FormItem status

Just posting for everybody’s benefit.
I am testing with gwt 2.0.3 and smartgwt 2.1 and ran into this.

I create a CheckboxItem but do NOT add it to a form:

final StaticTextItem idItem = new StaticTextItem ();
idItem.setTitle ("Id");

final CheckboxItem checkboxItem = new CheckboxItem ();
checkboxItem.setTitle ("Enabled");
checkboxItem.setValue (true);

final DynamicForm form = new DynamicForm ();
form.setFields (idItem/*, checkboxItem*/);

Later, such as in response to a button being pressed, I call checkboxItem.getValue (), and get this:

02:17:28.609 [ERROR] <Tue Apr 20 12:05:48 PDT 2010> <ERROR> <StandardUpdateEntityTab$3> <>com.google.gwt.core.client.JavaScriptException: (TypeError): Object #<an Object> has no method 'getValue'  stack: TypeError: Object #<an Object> has no method 'getValue'
 at [object Object].<anonymous> (unknown source)
 at __gwt_jsInvoke (http://127.0.0.1:8888/portal/hosted.html?portal:70:35)
 at eval
 at <anonymous> (http://127.0.0.1:8888/portal/hosted.html?portal:54:12)
 at Object.<anonymous> (unknown source)
 at unknown source
 at __gwt_jsInvoke (http://127.0.0.1:8888/portal/hosted.html?portal:70:35)
 at eval
 at <anonymous> (http://127.0.0.1:8888/portal/hosted.html?portal:54:12)
 at Object.click (unknown source)
 at Object.handleActivate (http://127.0.0.1:8888/portal/sc/modules/ISC_Foundation.js:296:108)
 at Object.handleClick (http://127.0.0.1:8888/portal/sc/modules/ISC_Foundation.js:298:13)  type: undefined_method  arguments: getValue,[object Object]  __gwt_ObjectId: 4269
 at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:195)
 at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:120)
 at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:507)
 at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:264)
 at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
 at com.smartgwt.client.widgets.form.fields.FormItem.getValue(FormItem.java)
 at org.portal.web.client.user.page.EditUserProfileTab.createEntityModelFromForm(EditUserProfileTab.java:203)
 at org.portal.web.client.user.page.EditUserProfileTab.createEntityModelFromForm(EditUserProfileTab.java:1)
 at org.portal.web.client.mvp.StandardUpdateEntityTab$3.execute(StandardUpdateEntityTab.java:157)
 at org.portal.web.client.core.callback.DefaultAsyncCallback.executeWorkflow(DefaultAsyncCallback.java:120)
 at org.portal.web.client.core.callback.AsyncCallbackClickHandler.onClick(AsyncCallbackClickHandler.java:25)
 at com.smartgwt.client.widgets.events.ClickEvent.dispatch(ClickEvent.java:96)
 at com.smartgwt.client.widgets.events.ClickEvent.dispatch(ClickEvent.java:1)
 at com.google.gwt.event.shared.HandlerManager$HandlerRegistry.fireEvent(HandlerManager.java:65)
 at com.google.gwt.event.shared.HandlerManager$HandlerRegistry.access$1(HandlerManager.java:53)
 at com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:178)
 at com.smartgwt.client.widgets.BaseWidget.fireEvent(BaseWidget.java:71)
 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.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
 at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
 at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
 at com.google.gwt.dev.shell.BrowserChannel.reactToMessagesWhileWaitingForReturn(BrowserChannel.java:1713)
 at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:165)
 at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:120)
 at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:507)
 at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:264)
 at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
 at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
 at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:188)
 at sun.reflect.GeneratedMethodAccessor185.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
 at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
 at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
 at com.google.gwt.dev.shell.BrowserChannel.reactToMessages(BrowserChannel.java:1668)
 at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:401)
 at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:222)
 at java.lang.Thread.run(Thread.java:619)

The problem goes away if I change the code to always add the CheckboxItem to the form, and hiding it when I do not want the user to see it:

final StaticTextItem idItem = new StaticTextItem ();
idItem.setTitle ("Id");

final CheckboxItem checkboxItem = new CheckboxItem ();
checkboxItem.setTitle ("Enabled");
checkboxItem.setValue (true);

final DynamicForm form = new DynamicForm ();
form.setFields (idItem, checkboxItem);

// paint the form
final VLayout layout = new VLayout ();
layout.addMember (form);
setPane (layout);

// hide the privileged controls
if (hidePrivilegedFieldsFlag) {
  checkboxItem.hide ();
}