Wednesday, July 18, 2007

NullPointerException tomcat5 realWriteChars

NullPointerException tomcat5 realWriteChars servlet

Getting this stack trace on each access of a servlet:

 at org.apache.coyote.tomcat5.OutputBuffer.realWriteChars(
 at org.apache.tomcat.util.buf.CharChunk.flushBuffer(
 at org.apache.tomcat.util.buf.CharChunk.append(
 at org.apache.coyote.tomcat5.OutputBuffer.write(
 at org.apache.coyote.tomcat5.CoyoteWriter.write(
 at org.apache.coyote.tomcat5.CoyoteWriter.write(
 at org.apache.coyote.tomcat5.CoyoteWriter.print(
 at org.apache.coyote.tomcat5.CoyoteWriter.println(
 at com.example.MyServlet.doGet(

The line number in "MyServlet" code that's kicking this off is a simple PrintWriter.println() ... what it's writing to the stream is definitely not null. How could a NPE be caused in tomcat?

I wouldn't have guessed at the issue if not trying the same servlet on WebSphere ... then you get a more useful error message:
Invalid character encoding "UTF=8"

There's a typo (ie '=' instead of '-') in the call to set the content type on the HttpResponse object! Correcting this to "UTF-8" fixes the issue:
response.setContentType("text/html; charset=UTF-8");

No comments: