Suggestion: Different Handling of FormItem.getAttribute() and Related Methods

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.

$0.02; I hope this is useful feedback.

Regards,

 Kevin Hardiman

Thanks for the feedback. Makes sense - I’ll make this change.

Sanjiv

Sanjiv,

If you could, let me know when this has been changed in the trunk and I’ll test it.

Thanks,

 Kevin Hardiman

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?

Thanks,
Sanjiv

Sanjiv,

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");
	}

}

If you need more, let me know.

 Kevin Hardiman