Hi,

You've not posted the exact error message.
So here's a guess:

See the following line in your code.

Integer itemcount = (Integer) session.getValue("itemcount");

Here, session.getValue() returns an object. If this object is null, it will
throw a NullPointerExceltion when you try to type cast it to an Integer.

Instead, try this:

Integer itemCount;
Object anObject = session.getValue("itemcount");
if(anObject==null)
   itemCount = new Integer(0);
else
   itemCount = (Integer)anObject;

Another problem area:
>     System.out.println (selected.length);
>         if (selected != null){

When you do a System.out.println() for the length of the "selected" array, what
if the "selected" object reference points to null ? Then the if statement will
not be of much use.

And another:
you're maintaining a count of integers in the for loop.
Instead of creating a new Integer each time, increment a simple int variable's
value. And then when you're done, set this int variable into the Integer
itemcount, and then set this into the session. This way, your code won't become
slow due to wasteful object creation, when you create a new Integer() each
time...


If this does not solve your problem, please post the full stacktrace.
This will help somebody give you a better solution.

Some observations:
1. Are you using the Servlet 2.2 or 2.3 API ? If so, then please use
session.getAttribute() and session.setAttribute() as the session.getVAlue() and
session.setValue() are deprecated methods from servlet API 2.2 onwards.

2.


Sriram

--- Wasala Mudiyanselage Nilantha Piyasiri <[EMAIL PROTECTED]> wrote:
> following servlet gives a errornull message. Please help me to overcome.
>
> Regards
> Nilantha
>
>
> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.io.*;
> import java.util.*;
>
> public class cartExample extends HttpServlet{
>     HttpSession session = null;
>
>     public void doPost (HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException{
>         try {response.setContentType("text/html");
>         PrintWriter out = new PrintWriter (response.getOutputStream());
>         out.println("<html>");
>         out.println("<head><title>Servlet</title></head>");
>         out.println("<body>");
>         out.println ("Here are the ordered Items");
>
>         session = request.getSession(true);
>
>         Integer itemcount = (Integer) session.getValue("itemcount");
>
>         if (itemcount == null){
>             itemcount = new Integer(0);
>         }
>
>         String [] selected = request.getParameterValues("item");
>         System.out.println (selected.length);
>         if (selected != null){
>             for (int i= 0; i<selected.length; i++){
>                 if (selected[i]!=null){
>                     String name = selected [i];
>                     System.out.println ("not null");
>                     itemcount = new Integer (itemcount.intValue()+1);
>                     session.putValue(Integer.toString(i).trim(),new
> String(name));
>                 }
>             }
>
>             session.putValue("item count", itemcount);
>         }
>
>         System.out.println (itemcount.intValue());
>         out.println("The products are");
>         for (int i=0; i <itemcount.intValue(); i++){
>             if (session.getValue(Integer.toString(i).trim())!=null){
>                 String item = (String)
> session.getValue(Integer.toString(i).trim());
>                 out.print ("<p>" + item);
>             }
>         }
>         out.println ("</body></html>");
>         out.close();
>     }
>
>     catch(Exception e){
>         System.out.println ("error" + e.getMessage());
>     }
> }
>     public void destroy (){
>         session = null;
>     }
> }
>


__________________________________________________
Do You Yahoo!?
Check out Yahoo! Shopping and Yahoo! Auctions for all of
your unique holiday gifts! Buy at http://shopping.yahoo.com
or bid at http://auctions.yahoo.com

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to