calling request.getHEades("user-agent") twice ->NPE?!

S

Stefanie Ertheld

Hi,

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().

(I know I could improve the code but thats not the point here)

The api for getHeaders says:
<API>
getHeaders

public java.util.Enumeration getHeaders(java.lang.String name)

Returns all the values of the specified request header as an
Enumeration of String objects.

Some headers, such as Accept-Language can be sent by clients as
several headers each with a different value rather than sending the
header as a comma separated list.

If the request did not include any headers of the specified name,
this method returns an empty Enumeration. The header name is case
insensitive. You can use this method with any request header.

Parameters:
name - a String specifying the header name
Returns:
an Enumeration containing the values of the requested header.
If the request does not have any headers of that name return an empty
enumeration. If the container does not allow access to header
information, return null
</API>

So the important part is probably:
1. If the request did not include any headers of the specified name,
this method returns an empty Enumeration.

2. If the container does not allow access to header information, return null
------
But I don't get why requesting the same header info twice would not be
allowed?!

Any ideas?

Thanks in advance,

Stefanie
 
G

GArlington

Hi,

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().

(I know I could improve the code but thats not the point here)

The api for getHeaders says:
<API>
getHeaders

public java.util.Enumeration getHeaders(java.lang.String name)

Returns all the values of the specified request header as an
Enumeration of String objects.

Some headers, such as Accept-Language can be sent by clients as
several headers each with a different value rather than sending the
header as a comma separated list.

If the request did not include any headers of the specified name,
this method returns an empty Enumeration. The header name is case
insensitive. You can use this method with any request header.

Parameters:
name - a String specifying the header name
Returns:
an Enumeration containing the values of the requested header.
If the request does not have any headers of that name return an empty
enumeration. If the container does not allow access to header
information, return null
</API>

So the important part is probably:
1. If the request did not include any headers of the specified name,
this method returns an empty Enumeration.

2. If the container does not allow access to header information, return null
------
But I don't get why requesting the same header info twice would not be
allowed?!

Any ideas?

Thanks in advance,

Stefanie

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...
 
L

Lew

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.)
 
D

Daniel Pitts

Lew said:
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.)
Also, showing us the stack trace might help us diagnose the problem.
 
S

Stefanie Ertheld

A better test is to use a debugger or /two/ assignments from the
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top