3

When I run Java servlet first time on server, all web-pages work good and there is no problem. But when I stop the server, restart it and run the servlet again, there is a null pointer exception on one page. I tried to print something, where this error was occurred , but when I write System.out.printl("something") there, and run then again the servlet (restarting server many times) there is no more exception thrown.

Can anyone help to fix this problem?

Here is doPost method where is the exception thrown

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ShoppingCart cart = (ShoppingCart)request.getSession().getAttribute("Cart");
    ProductCatalog pc = (ProductCatalog) request.getServletContext().getAttribute("Product Catalog");
    String id = request.getParameter("productID");
    if(id != null){
        Product pr = pc.getProduct(id);
        cart.addItem(pr); // here is null pointer exception
    }
    RequestDispatcher dispatch = request.getRequestDispatcher("shopping-cart.jsp");
    dispatch.forward(request, response);
}

and here is cart class:

private ConcurrentHashMap items;

/** Creates new Shopping Cart */
public ShoppingCart(){
    items = new ConcurrentHashMap<Product, Integer>();
}

/** Adds a product having this id into cart and increases quantity. */
//This method is called after "add to cart" button is clicked. 
public void addItem(Product pr){
    System.out.println(pr.getId() + " sahdhsaihdasihdasihs");
    if(items.containsKey(pr)){
        int quantity = items.get(pr) + 1;
        items.put(pr, quantity);
    } else {
        items.put(pr, 1);
    }
}

/** 
 * Adds a product having this id into cart and 
 * increases quantity with the specified number. 
 * If quantity is zero, we remove this product from cart.
 */
//This method is called many times after "update cart" button is clicked. 
public void updateItem(Product pr, int quantity){
    if(quantity == 0)items.remove(pr);
    else items.put(pr, quantity);
}

/** Cart iterator */
public Iterator<Product> cartItems(){
    return items.keySet().iterator();
}

/** Returns quantity of this product */
public int getQuantity(Product pr){
    return items.get(pr);
}
1
  • 9
    Hi Kote, we're not so much worried about English as we are the lack of code. We all speak code :) So if you post it, we have a better chance of helping you. Good luck! P.S. Click edit beneath your question so you can add your code to the body of the question. Commented May 12, 2012 at 8:30

2 Answers 2

1

If the exception is thrown here ...

    cart.addItem(pr);

... that is because cart is null. The most likely explanation is that this call returns null.

    request.getSession().getAttribute("Cart");

That would be happening because the session doesn't (yet) contain a "Cart" entry. What you need to do is test the result of that call. If it returns null, you need to create a new ShoppingCart object, and use `Session.setAttribute(...) to add it to the session object for the next request that uses the session.

Sign up to request clarification or add additional context in comments.

3 Comments

i have this in session class: public void sessionCreated(HttpSessionEvent arg0) { ShoppingCart cart = new ShoppingCart(); arg0.getSession().setAttribute("Cart", cart); } why it is not enough?
it works, thank you. but i didn't understand why it returns null on the second time
Could you mark this as the correct answer. It helped me and I'm sure it'll help others who stumble upon it.
0

When you restart I'm guessing you're resubmitting the form which is causing the null pointer, this is either happening when you try to retrieve the session or when you get the Cart attribute. When you call cart.addItem the cart object is null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.