Some background: I have a DynamicForm with a series of FormItem(s), some of which have some metadata associated with them (but not all them). I am associating the metadata by setting a particular attribute - FormItem.setAttribute(key, value).
When I attempt to retrieve an attribute from a FormItem that doesn’t have an attribute set, I get either a NullPointerException, or some other. I don’t see this as an “Exception” case, and as such would recommend that instead simply “null” is returned. I can simply catch Exception and swallow it, but I’d prefer not to.
This appears to be true for FormItem.getAttributeAsInt(), and I would imagine the other type-casted methods as well.
Kevin,
The code seems to be doing this already. I tried to reproduce it by modifying a Showcase form example and it correctly returns null if the attribute is not found.
Can you post a simple test case, or a modification of a showcase example that reproduces the issue?
Here’s a very simple example of creating a TextItem, adding it to a DynamicForm, and then attempting to fetch a non-existent attribute as an Integer. You should see (as I do) that it will not run, as the assignment throws a NullPointerException:
public class TestEntryPoint implements EntryPoint {
public void onModuleLoad() {
Canvas canvas = new Canvas();
canvas.setWidth100();
canvas.setHeight100();
TextItem text = new TextItem("Text");
text.setTitle("Text Item");
DynamicForm form = new DynamicForm();
form.setItems(text);
canvas.addChild(form);
canvas.draw();
//FIXME: the following assigment will thrown NullPointerException
int value = text.getAttributeAsInt("nonExistentAttribute");
}
}