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
java.lang.NullPointerException
at org.apache.struts.taglib.TagUtils.retrieveMessageResources(TagUtils.java:1175)
at org.apache.struts.taglib.TagUtils.message(TagUtils.java:1038)
at org.apache.struts.taglib.TagUtils.message(TagUtils.java:1013)
at org.apache.struts.taglib.bean.WriteTag.retrieveFormatString(WriteTag.java:254)
at org.apache.struts.taglib.bean.WriteTag.formatValue(WriteTag.java:360)
at org.apache.struts.taglib.bean.WriteTag.doStartTag(WriteTag.java:232)
.... (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="" />.]