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



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

GArlington wrote:
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.)

--
Lew
.