Archive for July, 2008

NullReferenceException while using bean:write

Wednesday, July 2nd, 2008

Here's an obscure error that I fought for several hours today:

Jul 2, 2008 5:42:42 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
at org.apache.struts.taglib.TagUtils.retrieveMessageResources(
at org.apache.struts.taglib.TagUtils.message(
at org.apache.struts.taglib.TagUtils.message(
at org.apache.struts.taglib.bean.WriteTag.retrieveFormatString(
at org.apache.struts.taglib.bean.WriteTag.formatValue(
at org.apache.struts.taglib.bean.WriteTag.doStartTag(
.... (the rest is unimportant)

This was thrown by the following line in a JSP:

<bean:write name="report" property="exceptionReportId" />

The problem is that my method getExceptionReportId() returns an int. The struts bean tag library expects all properties to be Strings. So by changing my method to return a String and using Integer.toString, I made it happy.

This presents a real separation-of-concerns problem. A bean should be a dumb data container. It should give you back exactly what you put in, and it should preserve your data types. The restriction that all properties are strings puts formatting into the bean, and therefore makes it specific to the JSP page.

Is there a tag library that separates formatting concerns from beans?

[UPDATE: You can format non-String property values using the bean:write format attribute. For a date, for example, <bean:write name="report" property="reportDate" format="MM/dd/yyyy"/>. Furthermore, to fix the NullPointerException above, I just added an empty format attribute, as in <bean:write name="report" property="exceptionReportId" format="" />.]