wood burning stoves 2.0*
The moose likes Servlets and the fly likes help needed with tomcat/requestDispatcher - example form HeadFirst servlets and jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "help needed with tomcat/requestDispatcher - example form HeadFirst servlets and jsp" Watch "help needed with tomcat/requestDispatcher - example form HeadFirst servlets and jsp" New topic
Author

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

Arul Jose
Ranch Hand

Joined: Jan 14, 2005
Posts: 131
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
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
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


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Arul Jose
Ranch Hand

Joined: Jan 14, 2005
Posts: 131


Thank you guys.

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

Thanks.

ARUL JOSE.
 
jQuery in Action, 2nd edition
 
subject: help needed with tomcat/requestDispatcher - example form HeadFirst servlets and jsp