Getting Null Pointer Excpetions while Viewing cart through Hidden values

Hi Guys,
I have created a demo cart using hidden values but as I am trying to view cart..
I am getting following errors

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
      com.viewcart.processRequest(viewcart.java:47)
      com.viewcart.doGet(viewcart.java:73)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:305)


note The full stack trace of the root cause is available in the Tomcat logs.


My code is ----

shopcart.class(Servlet)
-------------------------------------
/*
 * shopcart.java
 *
 * Created on March 4, 2007, 12:50 PM
 */

package com;

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

/**
 *
 * @author  Amit
 * @version
 */
public class shopcart extends HttpServlet {
   
   
    Vector v;
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        v= new Vector();
       
    }
   
    /** Destroys the servlet.
     */
    public void destroy() {
       
    }
   
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
       
       
        out.println("<HTML><BODY>");
        out.println("<FORM ACTION=\"./viewcart\"METHOD=GET>");
        out.println("<INPUT TYPE= TEXT NAME=CART");
        out.println("<INPUT TYPE= HIDDEN NAME="+v+"<br>");
        out.println("</FORM>");
       
        Iterator i= v.iterator();
       
        while(i.hasNext())
             out.println(i.next()+"<br>");
       
        out.println("<BODY><HTML>");
        /* TODO output your page here
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet</title>");
        out.println("</head>");
        out.println("<body>");
         
        out.println("</body>");
        out.println("</html>");
         */
        out.close();
    }
   
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
   
}

and viewcart.class(Servlet)
-------------------------------
/*
 * viewcart.java
 *
 * Created on March 4, 2007, 12:50 PM
 */

package com;

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

/**
 *
 * @author  Amit
 * @version
 */
public class viewcart extends HttpServlet {
   
    Vector v1;
    /** Initializes the servlet.
     */
    ServletContext sc;
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        Vector v1= new  Vector();
        sc= config.getServletContext();
    }
   
    /** Destroys the servlet.
     */
    public void destroy() {
       
    }
   
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
       
        v1.add(request.getParameter("CART"));
        try
        {
        v1= (Vector)request.getAttribute("v");
        }
        catch(Exception e)
        {
            System.out.println("Ankur");
            out.println("The Exception is :<br>");
            out.println(e.getCause());
        }
       
        request.setAttribute("v",v1);
       
        RequestDispatcher rd= sc.getRequestDispatcher("./shopcart");
        rd.forward(request,response);
       
        out.close();
    }
   
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
   
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
   
}
dojjolAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
which is line 47?
Mick BarryJava DeveloperCommented:
>   out.println("<INPUT TYPE= HIDDEN NAME="+v+"<br>");

looks like that should be:

  out.println("<INPUT TYPE= HIDDEN NAME=\"v\"/><br>");
Mick BarryJava DeveloperCommented:
ignore that, acually not sure what your intention is there

>    v1= (Vector)request.getAttribute("v");

that won't work, your Vector does not get added to the request.
Looks like you should be storing the Vector in the session instead


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

dojjolAuthor Commented:
Object,
I am trying to actually use Vector-v as the name for the hidden variable so that it can be used by viewcart servlet and I can store the value retireved from the "CART" Textfield of showcart servlet and I can add "CART" value and then send it back to showcart servlet .

Dojjol
fargoCommented:
hi,

In shopCart servlet, you are doing the following

   out.println("<INPUT TYPE= HIDDEN NAME="+v+"<br>");

here v is defined as Vector. What are you trying to do here? If you assign the value from the vector to the value of the hidden field, it makes sense.

Next, in the viewCart servlet, you are just getting the "CART" parameter and adding it to vector and setting it to request. Why? Why don't just get the "CART" parameter directly in the shopCart servlet. I don't see the purpose of viewCart here.

Explaination?

regards,
fargo
evnafetsCommented:
The null pointer exception is coming from line 47 of viewCart.java - NOT shopcart.
The offending line of code is:
v1.add(request.getParameter("CART"));

The vector v1 is null.  It has never been initialised.

Note in your init method: you declare a different Vector v1, and initialise that one.  You never touch the variable this.v1.
Just to make it explicit:

public void init(ServletConfig config) throws ServletException {
        super.init(config);
        this.v1= new  Vector();
        this.sc= config.getServletContext();
    }

That explains your null pointer exception.

Going on from this, having class attributes in a servlet is a big no-no.  They are not thread safe, and will be shared by all users of this servlet.
The init method is only called once, when you start up the server.
Only one instance of the servlet is ever created, and it handles all the requests (multi-threading)

If you want individual users to have their own "carts" you should be using session attributes.
dojjolAuthor Commented:
Hi,
I was trying to use Hidden Parameters for Session tracking,because I am not able to find a scence where hidden parameters can considered best for session tracking.

Plz give me any eamples if u have.

Dojjol
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.