Stefanie Ertheld wrote:
>> I got the following line of code:
>> if (request.getHeaders("user-agent") != null &&
>> request.getHeaders("user-agent").nextElement() != null)
>>
>> Which throws a NullPointerException at .nextElement().
> Try to assign request.getHeaders("user-agent") to some var and see
> what is the value returned, it is probably empty Enumeration (as in 1.
> above), then your call to nextElement() on empty Enumeration is what
> causing the problem...
Nope.
If request is not null, and the second getHeaders() call does not return null,
then the expression with nextElement() would not throw an NPE.
A better test is to use a debugger or /two/ assignments from the getHeaders()
call to see if the second one returns null. I don't know why it would, but
maybe it does. (I would use a debugger myself; I avoid rewriting code just to
debug it, then rewriting it back.)

Signature
Lew
Daniel Pitts - 02 Feb 2008 23:17 GMT
> Stefanie Ertheld wrote:
>>> I got the following line of code:
[quoted text clipped - 17 lines]
> why it would, but maybe it does. (I would use a debugger myself; I
> avoid rewriting code just to debug it, then rewriting it back.)
Also, showing us the stack trace might help us diagnose the problem.

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Stefanie Ertheld - 03 Feb 2008 13:52 GMT
>> A better test is to use a debugger or /two/ assignments from the
>> getHeaders() call to see if the second one returns null.
Can't use a debugger cause the code was already replaced by
request.getHeader instead, which is a workarround - and I am not
supposed to spend more work on this topic - it's just that I personally
am interested in finding out what went wrong to learn from it and to
prevent such error the next time.
> Also, showing us the stack trace might help us diagnose the problem.
java.lang.NullPointerException
at
org.apache.tomcat.util.http.ValuesEnumerator.nextElement(MimeHeaders.java:423)
at
com.mycompany.myproject.mypackage.MySpringController.myMethod(MySpringController.java:453)
at
com.mycompany.myproject.mypackage.MySpringController.handleRequest(MySpringController.java:169)
at
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:684)
at
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
at
org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:386)
at
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:346)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:526)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at
org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:307)
at
org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:385)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:748)
at
org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:678)
at
org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:871)
at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run()V(Unknown Source)
Thanks in advance,
Stefanie