Try debugging.
You have two options.
1) Insert System.out.println() statements in your code printing out references that might be null. This should point you in the right direction.
2) Use the Java Debugger (jdb) to debug your servlet. Start as jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=8000
Make sure you've started Tomcat as ./catalina.sh jpda run
(JPDA means debugging is turned on).
Then place a breakpoint in your code in a place you know is still working. Then step through your code from there. (Use 'next' to go to the next line of code on the same level, 'step' to step into a method).
Of course, you could also use a combination of 1) and 2).
Good luck, Frans
Vamsee Kanakala wrote:
Hello list users,
Maybe this is not the best place to ask a general servlet doubt, but I'm hoping someone can point out the mistake I'm making. I'm attaching a servlet file (FetchEmployeeServlet.java), which for some strange reason, gives a Null-Pointer Exception when accessing a function. This tells me that the class is not instantiated or something, but I'm new to servlets, so I can't figure out why.
TIA, Vamsee.
------------------------------------------------------------------------
package com.vamsee.empdb;
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; import javax.sql.*; import java.util.*; import javax.naming.*;
public class FetchEmployeeServlet extends HttpServlet { private Connection conn = null; private ServletContext context; private PreparedStatement pstmt = null;
public void init(ServletConfig config) throws ServletException { super.init(config); context = config.getServletContext();
try { Context initContext = new InitialContext(); Context envContext = (Context)initContext.lookup("java:comp/env"); if (envContext == null) throw new Exception("Panic: No Context!");
DataSource ds = (DataSource)envContext.lookup("jdbc/empdb");
if (ds != null) { Connection conn = ds.getConnection(); } } catch (Exception e) { e.printStackTrace(); } }
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String jsp; String cmd = req.getParameter("cmd"); String idString = req.getParameter("id"); int id;
try { id = Integer.parseInt(idString); } catch (NumberFormatException e) { id = 0; }
if ("get".equals(cmd)) { EmployeeBean bean = this.fetchEmployee(id); req.setAttribute("employee", bean); jsp = "/employee.jsp"; } else { List list = this.fetchAll(); req.setAttribute("list", list); jsp = "/list.jsp"; } RequestDispatcher dispatcher; dispatcher = context.getRequestDispatcher(jsp); dispatcher.forward(req, res); }
public EmployeeBean makeBean(ResultSet results) throws SQLException {
EmployeeBean bean = new EmployeeBean(results.getInt("id")); bean.setFirstName(results.getString("fname")); bean.setLastName(results.getString("lname")); bean.setEmail(results.getString("email")); bean.setDepartment(results.getString("department")); bean.setImage(results.getString("image"));
return bean; }
public EmployeeBean fetchEmployee(int id) { EmployeeBean bean = null; try { ResultSet results; String sql = "select * from people_table where id = ?"; PreparedStatement pstmt = conn.prepareStatement(sql);
synchronized (pstmt) { pstmt.clearParameters(); pstmt.setInt(1, id); results = pstmt.executeQuery(); }
if (results.next()) bean = makeBean(results);
if (results != null) results.close();
} catch (SQLException se) { se.printStackTrace(); }
return bean; }
public List fetchAll() { List list = new ArrayList(); try {
ResultSet results; Statement st = conn.createStatement(); System.out.println("Okay until create statement"); results = st.executeQuery("select * from people_table"); System.out.println("Okay until execute query");
while (results.next()) list.add(makeBean(results)); } catch (SQLException se) { se.printStackTrace(); } return list; }
public void destroy() { try { if (conn != null) conn.close(); } catch (SQLException e) { } } }
------------------------------------------------------------------------
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
-- Quinity : Your Partner In eBusiness Solution Delivery
Biltstraat 449 3572 AW Utrecht P.O. Box 13097 3507 LB Utrecht The Netherlands
Telephone: (+31) (0)30 2335999 Fax : (+31) (0)30 2335998 WWW : http://www.quinity.com ================================================================ The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer. Quinity is neither liable for the proper and complete transmission of the information contained in this communication nor for any delay in its receipt. ================================================================
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
