• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Shopping Cart - cannot add more than one item

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have following servlet to add items into cart -


One item gets added to the cart successfully. When adding the second item it gives a Null Pointer Exception at line 53

cart.add(item);

Can anyone please help me out with the problem.
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your NullPointerException is because 'cart' is null on this line:

To simplify things, let's just look at the code that deals with the 'cart' variable:

Can you see under what scenario 'cart' will be null?
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, welcome to the Ranch!
 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim.

Do I have to add cart = session.getAttribute("cart"); before cart.add(item);

 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you got it.

But... I wouldn't do it inside that if statement because you're going to run into the same NullPointerException when your action equals "2". I suggest doing it close to where you are doing the null check on the session attribute. Like so:
 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again. This time for quick replies.

I will try it. I will update the post after trying.
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Incidentally, this is poor design. It violates the http specifications. Decide which method you are going to use and let the other one throw a 405 Method Not Allowed error. Or if you want to use both methods in the same servlet (opinions vary here) then they should do distinctly different operations in keeping with the spec. Use GET to retrieve data (idempotent) and POST to submit data that will change the state of the application such as adding records to a database or items to a shopping cart (non idempotent).


And welcome to the Ranch!
 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kevin for your suggestion.

It does not give me any exception any more after the change. But it increments the quantity of the product added in the cart on browser refresh. How solve this issue?
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because you're using a GET request for adding things to the cart. You have a URL such as:
<your-server>/cart?act=1&productId=47&productName=PlushMoose&mrp=100&qty=3

Every time you submit that URL, you will put something in the cart.

A solution is to submit a POST request instead. Which brings us round nicely to what Kevin said yesterday, that you should use GET to retrieve data and POST to submit data, where submitting data is what you're doing in this case.

(Side note: Having the item price in the request is a bad idea. I could modify the request to put any price I want in there and you don't want people to be able to do that)
 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my JSP code to add item in the cart
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... and ... what's your question?
 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh...
I am not using GET method. I am using POST method with some hidden to pass to Servlet. Why does it increment the quantity of the added product in the cart?
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After you have submitted your form to add an item into the cart, what do you have in the browser address bar?
 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The address bar has something like bellow as you mentioned.

<your-server>/cart?act=1&productId=47&productName=PlushMoose&mrp=100&qty=3

Should I use JavaBean to pass parameter values to servlet instead of hidden field? Should it solve the issue?
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not have your code running so cannot verify, but this looks suspicious:

Did you mean:
?
 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right.
displayProducts.jsp displays all the products from selected category of products retrieved from database. here "cid" means category id. products are retrieved from database every time displayProduct.jsp is loaded.

is there anything wrong with this?
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does that change resolve your refresh form submission problem?
 
Shahid Kahn
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i gotta test it. i will update the post after testing. thanks.
it was a great help and support from you Tim.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic