Reputation: 147
I am trying to do a simple conversion from ArrayList to Object Array but I am getting an error, I have tried numerous methods as mention on the internet but none of them is working, NEED HELP
Here is what I am doing
private List<CartItems> cartobj = new ArrayList<CartItems>();
--- Bean (Cart) ---
private String name;
private float price;
private int quantity;
--- In Servlet ---
Order h = new Order(String a, String b, String c);
cartobj.add(h);
session.setAttribute("Cart", cartobj);
--- In JSP ---
<tbody>
<% ArrayList<CartItems> cartobj = new ArrayList<CartItems>();
cartobj.add((CartItems)session.getAttribute("Cart"));
for(int i=0;i<cartobj.size();i++)
{%>
<tr>
<td><% out.println(i+1); %></td>
<td><% cartobj.get(i).getProductName(); %></td>
<td><% cartobj.get(i).getPrice(); %></td>
<td><% cartobj.get(i).getSales_Address(); %></td>
<td><% cartobj.get(i).getOrder_Date(); %></td>
<td><% cartobj.get(i).getQuantity(); %></td>
</tr>
<% } %>
I also tried this
<%
CartItems[] obj = (CartItems[])session.getAttribute("Cart");
for(int i=0;i<obj.length;i++)
{%>
<tr>
<td><% out.println(i+1); %></td>
<td><% obj[i].getProductName(); %></td>
<td><% obj[i].getPrice(); %></td>
<td><% obj[i].getSales_Address(); %></td>
<td><% obj[i].getOrder_Date(); %></td>
<td><% obj[i].getQuantity(); %></td>
</tr>
<% } %>
I am getting this following error
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Final_Project] threw exception [java.lang.ClassCastException: java.util.ArrayList cannot be cast to [LObjects.CartItems;] with root cause
java.lang.ClassCastException: java.util.ArrayList cannot be cast to [LObjects.CartItems; at org.apache.jsp.cart_jsp._jspService(cart_jsp.java:165)
Upvotes: 2
Views: 4786
Reputation: 1669
You need to cast it to List like this:
List<CartItems> obj = (List<CartItems>) session.getAttribute("Cart");
Still better to use JSTL for directly iterating on your list of objects:
<c:forEach items="${Cart}" var="element">
<tr>
<td>${element.productName}</td>
<td>${element.salesAddress}</td>
<td>${element.quantity}</td>
</tr>
</c:forEach>
And follow Java bean naming conventions for your setters and getters, for example, getSales_Address is not a correctly defined.
Upvotes: 1
Reputation: 1491
You are setting List<CartItems>
to session, ArrayList
can not be cast with Array
.
Try the following:
<%
List<CartItems> obj = (List<CartItems>)session.getAttribute("Cart");
for(int i=0;i<obj.length;i++)
{
CartItems cartItem = obj.get(i);
%>
<tr>
<td><% out.println(i+1); %></td>
<td><% cartItem.getProductName(); %></td>
<td><% cartItem.getPrice(); %></td>
<td><% cartItem.getSales_Address(); %></td>
<td><% cartItem.getOrder_Date(); %></td>
<td><% cartItem.getQuantity(); %></td>
</tr>
<% } %
Avoid java code in your jsp as much as you can. You can achieve this using JSTL
.
<c:forEach items="${Cart}" var="c" varStatus="loop">
<tr>
<td>${loop.index+1}</td>
<td>${c.productName}</td>
<td>${c.price}</td>
<td>${c.sales_address}</td>
<td>${c.quantity}</td>
</tr>
</c:forEach>
Upvotes: 0
Reputation: 201507
You can't cast a List
to an array. A List
is not a type of array. But, you could use List.toArray(T[])
and change
session.setAttribute("Cart", cartobj);
to something like
session.setAttribute("Cart", cartobj.toArray(new CartItems[cartobj.size()]));
then
CartItems[] obj = (CartItems[])session.getAttribute("Cart");
would be valid.
Upvotes: 2