From: teser3 on 19 Apr 2010 18:01 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.
From: Lew on 19 Apr 2010 19:18 teser3(a)hotmail.com wrote: > 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. -- Lew
|
Pages: 1 Prev: NPE in Servlet Next: Way to sort / enforce order for Map.entrySet? |