0

I created a hashmap in my SERVLET as follows:

 int productId = Integer.parseInt(request.getParameter("productId"));

 HashMap cartList = new HashMap();
 Cart item = new Cart(productId, productName, price, quantity);
 cartList.put(productId, item);

But I have get the following error:

org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: The class 'java.util.HashMap$Entry' does not have the property 'productId'.

What does it mean? How can i resolve my error?

EDIT: HERE is my JSP

<c:forEach var="cart" items="${cartList}">
         ${cart.productId}
         ${cart.productName}
    <form method="POST" action="ShoppingCartUpdate">
        <input type="submit" value ="Update" class="loginButton" name="Update">
    </form>
    <form method="POST" action=""ShoppingCartRemove">
        <input type="submit" value ="Remove" class="loginButton" name="Delete">
    </form>
</c:forEach>
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
newbie
  • 14,582
  • 31
  • 104
  • 146

3 Answers3

3

When iterating the Map in JSTL you are iterating its Entrys, so that you need to use value property to access their values:

<c:forEach var = "e" items = "${cartList}">
    ${e.value.productId}
</c:forEach>
axtavt
  • 239,438
  • 41
  • 511
  • 482
1

You need to declare your HashMap as such so that it knows what the type of the key/value pair are. You should always instantiate Hashmaps in this way, and I'm not sure if it will let you even without doing so. I know that in things like Actionscript you can get away with defining a Dictionary and not what types need to be in it, but in Java you must define the types that are being used and you can't use primitive types (I believe) such as int, double etc

HashMap<Integer, Cart> cartList = new HashMap<Integer, Cart>(); 

and the productId must be Integer and not just int

Mike
  • 8,137
  • 6
  • 28
  • 46
  • Why do I need to declare it like that? How do I know if i need to declare it? Thank you. – newbie Mar 16 '11 at 13:05
  • I agree about the declaration part but not about the `int` vs `Integer` part. Java autoboxes int to Integer in this case. – adarshr Mar 16 '11 at 13:06
  • Okay I was pretty sure that I have had Java complain in the past about not declaring it as an Integer because it didn't allow primitive types as the key – Mike Mar 16 '11 at 13:08
1

It is due to the way you're trying to read it using a JSP or something.

$cartList[productId] should be done. Note that productId is an integer variable here.

adarshr
  • 61,315
  • 23
  • 138
  • 167