Can somebody pls explain me this error that I am getting when I try to run my web application on Tomcat.

java.lang.NullPointerException
business.Cart.addItem(Cart.java:27)
cart.CartServlet.doGet(CartServlet.java:49)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

The code for the Cart.java is

package business;

import java.io.Serializable;
import java.util.ArrayList;

public class Cart implements Serializable
{
	private ArrayList<LineItem> items;
	
	public Cart()
	{
		items = new ArrayList<LineItem>();
	}

	public ArrayList<LineItem> getItems()
	{
		return items;
	}

	public int getCount()
	{
		return items.size();
	}

	public void addItem(LineItem item)
	{
		String code = item.getProduct().getCode();
		int quantity = item.getQuantity();
		for(int i = 0; i<items.size(); i++)
		{
			LineItem lineItem = items.get(i);
			if(lineItem.getProduct().getCode().equals(code))
			{
				lineItem.setQuantity(quantity);
				return;
			}
		}
		items.add(item);
	}

	public void removeItem(LineItem item)
	{
		String code = item.getProduct().getCode();
		for(int i =0; i<items.size(); i++)
		{
			LineItem lineItem =items.get(i);
			if(lineItem.getProduct().getCode().equals(code))
			{
				items.remove(i);
				return;
			}
		}
		
	}
}

The code for the CartServlet.java is

package cart;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import business.*;
import data.*;


/**
 * Servlet implementation class CartServlet
 */
public class CartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String productCode = request.getParameter("productCode");
		String quantityString = request.getParameter("quantity");
		
		HttpSession session = request.getSession();
		Cart cart = (Cart) session.getAttribute("cart");
		if(cart==null)
			cart = new Cart();
		
		int quantity = 1;
		try
		{
			quantity = Integer.parseInt(quantityString);
			if(quantity < 0)
				quantity = 1;
		}
		catch(NumberFormatException nfe)
		{
			quantity = 1;
		}
		ServletContext sc = getServletContext();
		String path = sc.getRealPath("WEB-INF/products.txt");
		Product product = ProductIO.getProduct(productCode, path);
		
		LineItem lineItem = new LineItem();
		lineItem.setProduct(product);
		lineItem.setQuantity(quantity);
		if(quantity > 0)
			cart.addItem(lineItem);
		else if (quantity ==0)
			cart.removeItem(lineItem);
		
		session.setAttribute("cart", cart);
		String url = "/cart.jsp";
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
		dispatcher.forward(request, response);
		
	}

}

This Product product = ProductIO.getProduct(productCode, path); probably returned null. Put in some debug output to check the values of productCode, path, and product.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.