Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

help needed with tomcat/requestDispatcher - example form HeadFirst servlets and jsp

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i do not know why the following servlet program is not working. it says null pointer exception while reading from an object got from the request.

this is an example from head first servlets and jsp

THE HTML FILE
<html><body>
<h1 align="center">Beer Selection Page </h1>
<form method="post" action="SelectBeer.do">
Select beer characteristics <p>
Color :
<select name="color" size="1">
<option> light
<option> amber
<option> brown
<option> dark
</select>
<br><br>
<center>
<input type="submit">
</center>
</form></body></html>


THE SERVLET FILE
package com.example.web;

import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class BeerSelect extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selection Advice<br>");
String c = request.getParameter("color");

BeerExpert be = new BeerExpert();

List result = be.getBrands(c);


Iterator it = result.iterator();

while(it.hasNext()){
System.out.println(it.next());
}

HttpSession session = request.getSession(true);

session.setAttribute("style",result);


RequestDispatcher view = request.getRequestDispatcher("result.jsp");

view.forward(request,response);

}
}


THE MODEL CLASS FILE

package com.example.model;

import java.util.*;

public class BeerExpert {

public List getBrands(String color){
List brands = new ArrayList();

if(color.equals("amber")){
brands.add("Jack Amber");
brands.add("Red Moose");
}
else{package com.example.model;

import java.util.*;

public class BeerExpert {

public List getBrands(String color){
List brands = new ArrayList();

if(color.equals("amber")){
brands.add("Jack Amber");
brands.add("Red Moose");
}
else{
brands.add("Jail Pale Ale");
brands.add("Gout Stout");
}

return brands;

}
}

brands.add("Jail Pale Ale");
brands.add("Gout Stout");
}

return brands;

}
}

THE SERVLET FILE

<%@ page import="java.util.*" %>

<html>
<body>
<h1 align="center"> Beer Recommendations JSP </h1>
<p>

<%

ArrayList styles = (ArrayList) session.getAttribute("styles");
System.out.println(" hi hih "+request.getParameter("color"));

System.out.println("iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii " + styles);
Iterator it = styles.iterator();
//while(it.hasNext()){
//out.println("<br> try : "+it.next());
//}
%>
</body>
</html>

THE ERROR MESSAGE


HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
com.example.web.BeerSelect.doPost(BeerSelect.java:34)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.NullPointerException
org.apache.jsp.result_jsp._jspService(result_jsp.java:56)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
com.example.web.BeerSelect.doPost(BeerSelect.java:34)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
Apache Tomcat/5.0.28


Thanks in advance for the help.

jose
 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

ArrayList styles = (ArrayList) session.getAttribute("styles");



I think this is problem. You are setting style attribute in servlet and try to access attribute styles.

Thanks
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arul,
A couple tips:

If you are going to post more than a line or two of code in the forum,
wrap it in UBB CODE tags. This will preserve your indenting which will
make your code easier to read. This will generally result in more people
actually reading it and, thus, more people helping out.

When Tomcat parses and compiles your JSPs it keeps a copy of the generated servlet code in CATALINA_HOME/work.
The stack trace you posted gives you a specific line number in that generated code. Errors like this are pretty easy to track down if you have that source file open when you are reading the stack trace.

[ June 30, 2005: Message edited by: Ben Souther ]
 
Arul Jose
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thank you guys.

you have done a great help, reading all the code to find the fault.

Thanks.

ARUL JOSE.
 
reply
    Bookmark Topic Watch Topic
  • New Topic