Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Stefanie Ertheld - 31 Jan 2008 07:39 GMT
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
GArlington - 31 Jan 2008 10:53 GMT
> Hi,
>
[quoted text clipped - 46 lines]
>
> 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...
Lew - 31 Jan 2008 15:41 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.