0

I want to ask,

why java.lang.ClassCastException is triggered in my program ??

I am not sure the reason about this,

could anyone mind giving some advice for me?

Thanks a lot!!!

<%@ page contentType="text/html; language=java"%>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.lang.*"%>


<%!

public class Goods implements Comparable{

    private String Id = null;
    private String name = null;
    private float price = 0.00F;
    private int number = 0;
    private String percent = null;

    public Goods(String Id,String name,float price,int number,String percent){
        this.Id = Id;
        this.name = name;
        this.price = price;
        this.number = number;
        this.percent = percent;
    }
    public String getId()
    {
        return this.Id;
    }
    public String getName()
    {
        return this.name;
    }
    public float getPrice() 
    {
        return this.price;    
    }
    public int getNumber()
    {
        return this.number;    
    }
    public String getPercent()
    {
        return this.percent;
    }
    public int compareTo(Object m)
    {
        Goods n = (Goods)m;
        int comRs = Id.compareTo(n.Id);
        return comRs;
    }

}

%>

<%

        String id = "Comp232";
        String name = "OO_JAVA";
        int number = 1;
        float price= 222;
        String percent = "85%";

        Goods goods = new Goods(id,name,price,number,percent);
        //Goods shop ;
        ArrayList <Goods> ay = null;


        if((ArrayList)session.getAttribute("car")==null)
        {
            ay = new ArrayList <Goods> ();
            ay.add(goods);
            session.setAttribute("car",ay);

        }

        else
        {
            ay=(ArrayList)session.getAttribute("car");



            if(ay.isEmpty())
            {
                ay.add(goods);
                session.setAttribute("car",ay);
                //response.sendRedirect("order_index.jsp");
            }


            else
            {
                Iterator it = ay.iterator();

                //Object shop1 = it.next();


                for(int i = 0;i<ay.size();i++)  
                {
                    //this statement triggers java.lang.ClassCastException
                    //I am not sure what the problem 

                     Goods shop = (Goods)it.next();
                    //System.out.println(shop);

}}} 
/*
                    if(shop.compareTo(goods)==0)
                    {
                        out.println("textbook ordered");
                    }

                    else
                    {
                        ay.add(goods);
                        session.setAttribute("car",ay);

                    }
                }
            }
        }
    */
%>
2
  • 1
    Where's the traceback? Where's the line on which the exception is triggered? Commented Jan 25, 2011 at 15:42
  • Goods shop = (Goods)it.next(); this line has problem anyway, i have solved the problem, thanks Commented Jan 25, 2011 at 15:50

2 Answers 2

2

You need to use Iterator<Goods>.

Iterator<Goods> it = ay.iterator();

Also, you can use a foreach loop instead of using an Iterator. Functionally, it's the same thing, but it's a lot cleaner semantically.

for (Goods g in ay) {
    // do stuff
}

Lastly, I think

ay=(ArrayList)session.getAttribute("car");

should be

ay = (ArrayList<Goods>)session.getAttribute("car");
Sign up to request clarification or add additional context in comments.

1 Comment

Users with less than 15 rep aren't able to upvote. Accepting, however, should be possible.
1

this is suspect

 ay=(ArrayList)session.getAttribute("car");

are you sure you have set car as ArrayList in session

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.