2

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)

1
  • 2
    What about List<CartItems> obj = (List<CartItems>) session.getAttribute("Cart")? Commented Aug 24, 2015 at 4:18

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.