About a week ago, I reported what seemed like a sporadic null-pointer
exception from response.encodeUrl. I've managed to simplify the code that
provokes it, and will show it below. My original report, with details of
the Tomcat version and so on, is after this mail.
The error occurred in a large system I've written that uses servlets and
JSP. It enables users to analyse certain kinds of numerical data over the
Web, and uses sessions to hold information about those users currently
using the system. Below is a JSP page I've constructed that demonstrates
the error:
<%@ page import="javax.servlet.*,javax.servlet.http.*,salco.*"%>
<HTML>
<HEAD>
<TITLE>Test1</TITLE>
</HEAD>
<BODY BGCOLOR=white>
<H1>Test1</H1>
<%
HttpSession this_session = request.getSession(true);
out.println( "this_session = " + this_session + ".<BR>" );
out.println( "Date = " + new java.util.Date() + ".<BR>" );
ResponseHolder rh = (ResponseHolder)this_session.getAttribute(
"response_holder" );
out.println( "rh = " + rh + ".<BR>" );
if ( rh == null ) {
out.println( "rh is null.<BR>" );
rh = new ResponseHolder( response );
this_session.setAttribute( "response_holder", rh );
this_session.setAttribute( "count", 0 );
out.println( "<BR>count = 0.<BR>" );
}
else {
int count = (Integer)this_session.getAttribute( "count" );
out.println( "rh is not null.<BR>" );
this_session.setAttribute( "count", count+1 );
out.println( "<BR>count = " + (count+1) + ".<BR>" );
}
String result = "None yet";
try {
result = rh.response.encodeURL( "/Test1.jsp" );
out.println( "Encoded URL OK: result = " + result + ".<BR>" );
}
catch ( java.lang.NullPointerException e ) {
out.println( "Crashed while encoding URL: <BR>" );
e.printStackTrace( new java.io.PrintWriter(out) );
}
%>
</BODY>
</HTML>
This page starts by obtaining an instance of HTTPSession by calling
request.getSession(true). This instance will have been created when the
user logs in for their current session, by another call of
request.getSession(true).
The page then displays the date and time so that you can check that in my
diagnostics, the times follow one another and I'm not repeating old
output. It then tries to obtain an instance of a class I've defined named
ResponseHolder, and does so by calling HttpSession.getAttribute.
ResponseHolder is a class I wrote specially for this demo, and has one
field, an HTTPServletResponse. In real life, I store users' session data
in instances with many fields, of which an HTTPServletResponse is one. I
pass these to my server-side code. This needs the HTTPServletResponse when
doing such things as encoding URLs, which is why I include it. For the
purposes of this test, the non-HTTPServletResponse fields aren't relevant,
and so I've not included them in the ResponseHolder.
If the ResponseHolder instance is null, it's probably because the page is
being loaded for the first time in the session. In that case, I create a
new ResponseHolder, and call HttpSession.setAttribute to store it.
The page also maintains and displays a count. This is solely for this
demo. As with displaying the time, it's a sanity check on the output. For
example, it confirms that I actually have reloaded the page.
The rest of the page calls encodeURL, and displays the result. If
encodeURL crashes with a NullPointerException, it catches this and
displays the stack trace instead.
By the way, the page has three imports. Two are for the javax.servlet
classes; the third is for my project classes.
Next, here's my ResponseHolder class:
package salco;
import javax.servlet.*;
import javax.servlet.http.*;
public class ResponseHolder
{
public HttpServletResponse response;
public ResponseHolder( HttpServletResponse r )
{
this.response = r;
}
public String toString()
{
return "[ResponseHolder " + this.response + "]";
}
}
As mentioned earlier, it has just one field, the HttpServletResponse.
Finally, I'll show output from three sets of transactions. To create
these, I logged into the system, thereby creating a new session as
explained above. I then loaded the JSP page, Test1.jsp, and kept reloading
it. (This was in Firefox 4.0.1; but browser and browser version are
probably not relevant. The error also occurs on Internet Explorer and
Google Chrome.) I then copied the page shown by the browser into a text
file, and appended new pages as I reloaded them. Here, then is my
output. It's long, so be warned; to get to the end, search for "END OF
OUTPUT":
(FIRST SESSION)
Test1
this_session = org.apache.catalina.session.StandardSessionFacade@9be2b5.
Date = Wed Jun 15 17:46:45 BST 2011.
rh = null.
rh is null.
count = 0.
Encoded URL OK: result = /Test1.jsp.
Test1
this_session = org.apache.catalina.session.StandardSessionFacade@9be2b5.
Date = Wed Jun 15 17:46:55 BST 2011.
rh = [ResponseHolder org.apache.catalina.connector.ResponseFacade@9c176c].
rh is not null.
count = 1.
Crashed while encoding URL:
java.lang.NullPointerException at
org.apache.catalina.connector.Response.toAbsolute(Response.java:1594) at
org.apache.catalina.connector.Response.encodeURL(Response.java:1198) at
org.apache.catalina.connector.ResponseFacade.encodeURL(ResponseFacade.java:398)
at org.apache.jsp.Test1_jsp._jspService(Test1_jsp.java:93) at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at
java.lang.Thread.run(Unknown Source)
----
(SECOND SESSION)
Test1
this_session = org.apache.catalina.session.StandardSessionFacade@1ae9aaa.
Date = Wed Jun 15 17:50:22 BST 2011.
rh = null.
rh is null.
count = 0.
Encoded URL OK: result = /Test1.jsp.
Test1
this_session = org.apache.catalina.session.StandardSessionFacade@1ae9aaa.
Date = Wed Jun 15 17:50:35 BST 2011.
rh = [ResponseHolder
org.apache.catalina.connector.ResponseFacade@1f8bd0d].
rh is not null.
count = 1.
Encoded URL OK: result = /Test1.jsp.
Test1
this_session = org.apache.catalina.session.StandardSessionFacade@1ae9aaa.
Date = Wed Jun 15 17:50:46 BST 2011.
rh = [ResponseHolder
org.apache.catalina.connector.ResponseFacade@1f8bd0d].
rh is not null.
count = 2.
Encoded URL OK: result = /Test1.jsp.
Test1
this_session = org.apache.catalina.session.StandardSessionFacade@1ae9aaa.
Date = Wed Jun 15 17:51:44 BST 2011.
rh = [ResponseHolder
org.apache.catalina.connector.ResponseFacade@1f8bd0d].
rh is not null.
count = 3.
Encoded URL OK: result = /Test1.jsp.
Was OK up to and including count=94, then:
Test1
this_session = org.apache.catalina.session.StandardSessionFacade@1ae9aaa.
Date = Wed Jun 15 17:53:53 BST 2011.
rh = [ResponseHolder
org.apache.catalina.connector.ResponseFacade@1f8bd0d].
rh is not null.
count = 95.
Crashed while encoding URL:
java.lang.NullPointerException at
org.apache.catalina.connector.Response.toAbsolute(Response.java:1594) at
org.apache.catalina.connector.Response.encodeURL(Response.java:1198) at
org.apache.catalina.connector.ResponseFacade.encodeURL(ResponseFacade.java:398)
at org.apache.jsp.Test1_jsp._jspService(Test1_jsp.java:93) at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at
java.lang.Thread.run(Unknown Source)
----
(THIRD SESSION)
Test1
this_session = org.apache.catalina.session.StandardSessionFacade@c68a98.
Date = Wed Jun 15 18:36:38 BST 2011.
rh = null.
rh is null.
count = 0.
Encoded URL OK: result = /Test1.jsp.
Test1
this_session = org.apache.catalina.session.StandardSessionFacade@c68a98.
Date = Wed Jun 15 18:36:53 BST 2011.
rh = [ResponseHolder
org.apache.catalina.connector.ResponseFacade@121b59a].
rh is not null.
count = 1.
Encoded URL OK: result = /Test1.jsp.
Test1
this_session = org.apache.catalina.session.StandardSessionFacade@c68a98.
Date = Wed Jun 15 18:37:05 BST 2011.
rh = [ResponseHolder
org.apache.catalina.connector.ResponseFacade@121b59a].
rh is not null.
count = 2.
Encoded URL OK: result = /Test1.jsp.
Test1
this_session = org.apache.catalina.session.StandardSessionFacade@c68a98.
Date = Wed Jun 15 18:37:17 BST 2011.
rh = [ResponseHolder
org.apache.catalina.connector.ResponseFacade@121b59a].
rh is not null.
count = 3.
Encoded URL OK: result = /Test1.jsp.
Was then OK up to and including count=49, then:
Test1
this_session = org.apache.catalina.session.StandardSessionFacade@c68a98.
Date = Wed Jun 15 18:37:59 BST 2011.
rh = [ResponseHolder
org.apache.catalina.connector.ResponseFacade@121b59a].
rh is not null.
count = 50.
Crashed while encoding URL:
java.lang.NullPointerException at
org.apache.catalina.connector.Response.toAbsolute(Response.java:1594) at
org.apache.catalina.connector.Response.encodeURL(Response.java:1198) at
org.apache.catalina.connector.ResponseFacade.encodeURL(ResponseFacade.java:398)
at org.apache.jsp.Test1_jsp._jspService(Test1_jsp.java:93) at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at
java.lang.Thread.run(Unknown Source)
In this, the first session gave an error on the second use of the page,
i.e. the first reload. However, the second session didn't crash until the
93rd reload, and the third, until the 49th.
If this a bug, is there any chance of getting it fixed, or of getting a
circumvention? If I'm doing something bad, please tell me.
Jocelyn Ireson-Paine
http://www.j-paine.org
http://www.spreadsheet-parts.org
+44 (0)7768 534 091
Jocelyn's Cartoons:
http://www.j-paine.org/blog/jocelyns_cartoons/
On Thu, 9 Jun 2011, Jocelyn Ireson-Paine wrote:
Hi,
I'm getting sporadic null-pointer exceptions from 'response.encodeUrl'. This
is with Tomcat 7.0.14, the latest stable version to which I upgraded an hour
ago, and Java 1.6.0_26, which again I upgraded to at the same time, under
Windows XP. I also got the error under Tomcat 5.5.33, which is why I tried
upgrading. I uninstalled old Tomcats and Javas before upgrading.
Here's a trace of the error:
java.lang.NullPointerException
at
org.apache.catalina.connector.Response.toAbsolute(Response.java:1594)
at
org.apache.catalina.connector.Response.encodeURL(Response.java:1198)
at
org.apache.catalina.connector.ResponseFacade.encodeUrl(ResponseFacade.java:422)
at sharesim.ValueGame.encodeUrl(ValueGame.java:468)
The fourth line of the trace is my code.
To get some diagnostics, I encapsulated 'response.encodeUrl' in the following
method, and used 'println' to print to Tomcat's log. This method is the one
mentioned on the fourth line of the above trace.
private final static String encodeUrl( HttpServletResponse response
, String url
)
{
System.out.println( "encodeUrl" );
System.out.println( "response=" + response );
System.out.println( "url=" + url );
String result = null;
if ( response == null )
result = "not on Web";
else
result = response.encodeUrl( url );
System.out.println( "result=" + result );
return result;
}
As mentioned above, the errors are sporadic. My redirection code gets called
on my server when I submit one of my forms. But sometimes, the submit works;
sometimes it doesn't. Here are two successive traces from the above method:
encodeUrl
response=org.apache.catalina.connector.ResponseFacade@1a5ec6c
url=/ResearcherValueGame1.jsp
result=/ResearcherValueGame1.jsp
encodeUrl
response=org.apache.catalina.connector.ResponseFacade@1a5ec6c
url=/ResearcherValueGame1.jsp
The first one worked; the second crashed rather than returning the encoded
URL. The URL to be encoded is the same in both cases, and 'response' is
evidently the same instance. I don't know how to dump 'response' in order to
show relevant fields (whichever they are), but am happy to try it if someone
can suggest how.
I've found a few mentions on Google of such errors, but nothing definite
about it happening in Tomcat 7. One discussion which I thought I'd seen, but
can't find again, seemed to suggest that the problem occurred on version 5,
possibly because of a missing 'synchronized'. I've not seen anything in
recent postings to this list.
I wondered whether the problem might be caused by the browser creating
malformed cookies, or some such. (I'm using session-handling.) However, this
seems unlikely, as the error occurs regardless of whether I use Firefox,
Internet Explorer, or Google Chrome.
Any ideas?
Jocelyn Ireson-Paine
http://www.j-paine.org
Jocelyn's Cartoons:
http://www.j-paine.org/blog/jocelyns_cartoons/
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org