NPE in Servlet

T

teser3

Please advise reasons for NullPointerException in a servlet dealing
with request.getParameter in Tomcat 6.0.20.
The below does work where the city value does forward and show in my
JSP (pageOne.jsp or pageTwo.jsp).
But it also outputs the printStackTrace() NullPointerException on this
one line everytime: if(city.equals("Boston")).

public class ProServlet extends HttpServlet {
public void doGet (HttpServletRequest request,HttpServletResponse
response) throws ServletException, IOException
{
String city = request.getParameter("city");
request.setAttribute("city", city);
try {
if(city.equals("Boston"))
{
//forward to pageOne.jsp
}
else
{
//forward to pageTwo.jsp
}

}
catch(Exception e)
{
e.printStackTrace();
}


}
}


The NullPointerException can be eliminated if I do this:
String city = "Boston" so I assume something is wrong with the
request.getParameter.

I was also able to show the city value without NullPointerException in
the Servlet output page if I use the PrintWriter and comment out the
conditions:
public class ProServlet extends HttpServlet {
public void doGet (HttpServletRequest request,HttpServletResponse
response) throws ServletException, IOException
{
String city = request.getParameter("city");
request.setAttribute("city", city);
try {
/*
if(city.equals("Boston"))
{
//redirect to pageOne.jsp
}
else
{
//redirect to pageTwo.jsp
}
*/


PrintWriter out = res.getWriter();
out.println("Show city value " + city);
out.close();

}
catch(Exception e)
{
e.printStackTrace();
}

}
}


Please advise.
 
L

Lew

Please advise reasons for NullPointerException in a servlet dealing
with request.getParameter in Tomcat 6.0.20.
The below does work where the city value does forward and show in my
JSP (pageOne.jsp or pageTwo.jsp).

What do you mean by "forward"? What exactly did you do to prove that there
was a parameter entry for "city"?

It's obvious that you are not submitting a value for "city", hence you are
getting back 'null' for the value.
But it also outputs the printStackTrace() NullPointerException on this
one line everytime: if(city.equals("Boston")).

public class ProServlet extends HttpServlet {
public void doGet (HttpServletRequest request,HttpServletResponse
response) throws ServletException, IOException
{
String city = request.getParameter("city");
request.setAttribute("city", city);
try {
if(city.equals("Boston"))

Why not add a guard against 'null' here?
{
//forward to pageOne.jsp
}
else
{
//forward to pageTwo.jsp
}

}
catch(Exception e)
{
e.printStackTrace();
}


}
}


The NullPointerException can be eliminated if I do this:
String city = "Boston" so I assume something is wrong with the
request.getParameter.

Well, of course you're not going to get a NullPointerException if the pointer
is not null!
I was also able to show the city value without NullPointerException in
the Servlet output page if I use the PrintWriter and comment out the
conditions:
public class ProServlet extends HttpServlet {
public void doGet (HttpServletRequest request,HttpServletResponse
response) throws ServletException, IOException
{
String city = request.getParameter("city");
request.setAttribute("city", city);
try {
/*
if(city.equals("Boston"))
{
//redirect to pageOne.jsp
}
else
{
//redirect to pageTwo.jsp
}
*/


PrintWriter out = res.getWriter();
out.println("Show city value " + city);

And the result of this was ...?
out.close();

}
catch(Exception e)
{
e.printStackTrace();
}

}
}


Please advise.

Put in guard code against 'null'.

Your biggest clue is in the Javdocs for 'getParameter()':
"Returns the value of a request parameter as a String, or null if the
parameter does not exist."

The evidence is that the parameter does not exist.
 
J

Jukka Lahtinen

Lew said:
(e-mail address removed) wrote:

Why not add a guard against 'null' here?

And the most compact way to do it would be just to turn the condition
around to
if ("Boston".equals(city))

Admitted, this may seem a little bit less intuitive than
if (city!=null && city.equals("Boston"))
but it is shorter, and there's one comparison less.
 
L

Lew

Jukka said:
And the most compact way to do it would be just to turn the condition
around to
     if ("Boston".equals(city))

That's not a guard against 'null', that's a secret entrance for
'null'!
Admitted, this may seem a little bit less intuitive than
     if (city!=null && city.equals("Boston"))
but it is shorter, and there's one comparison less.

Of course, doing that obscures the fact that a 'null' value was
received, which from the OP's surprise violates a desired invariant.

Explicit checks for 'null' are better than that silly hackish idiom.

Prevention of 'null' to make 'city != null' an invariant (i.e.,
assertable) is best.
 

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,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top