NullPointerException with Servlet

Posted by RonaDona on Stack Overflow See other posts from Stack Overflow or by RonaDona
Published on 2012-11-04T16:40:07Z Indexed on 2012/11/04 17:00 UTC
Read the original article Hit count: 201

I am calling a Servlet using its URL address. This is the URL I am typing

http://localhost:7001/ryan/olympics?action=selectCatalog&id=1

This is the Servlet's URL for sure; if I change the address I get

page not found

This is the code for the Servlet.

public class Servlet extends javax.servlet.http.HttpServlet
        implements javax.servlet.Servlet {

private static final long serialVersionUID = 1L;

public Servlet() {
    super();
}

public void init(ServletConfig config) throws ServletException {
    System.out.println("*** initializing controller servlet.");
    super.init(config);
}

protected void doPost(HttpServletRequest request,
  HttpServletResponse response) throws ServletException, IOException {


String action = request.getParameter("action");



if (action.equals("selectCatalog")) {
      String categoryId = request.getParameter("id");

      ProductModelDAO dao4 = new ProductModelDAOImpl("jpac");
      if (categoryId != null && !categoryId.trim().equals("")) {
          CategoryDAO dao1 = new CategoryDAOImpl("jpac");

    try {
        Category category = dao1.getCategoryName(categoryId);
        request.setAttribute("category", category);
        } catch (Exception e) {
        e.printStackTrace();
        }
    }

    try {
        @SuppressWarnings("unchecked")
        List<Product> products = dao4
            .getProductsByCategory(categoryId);
        request.setAttribute("products", products);
    } catch (Exception e) {
        e.printStackTrace();
    }

    url = "SelectCatalog.jsp";

 RequestDispatcher requestDispatcher =
  getServletContext().getRequestDispatcher(url);
requestDispatcher.forward(request, response);

I get the NullPointerException pointing to the RequestDispatcher's line. Any help?

© Stack Overflow or respective owner

Related posts about java

Related posts about servlets