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