java.lang.ClassCastException问题

标签 java exception jsp

我想问

为什么我的程序中会触发java.lang.ClassCastException??

我不确定这是什么原因,

有人可以给我一些建议吗?

非常感谢!!!

<%@ 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);

                    }
                }
            }
        }
    */
%>

最佳答案

您需要使用Iterator<Goods> .

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

此外,您还可以使用 foreach 循环而不是使用迭代器。从功能上来说,它是一样的,但在语义上更清晰。

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

最后,我认为

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

应该是

ay = (ArrayList<Goods>)session.getAttribute("car");

关于java.lang.ClassCastException问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4795435/

相关文章:

java - Java 1.8 安装在 OS X El Capitan 上的什么位置?

java - 我用 Java 创建了自己的记录器。这是一个坏主意吗?

java - 在 servlet 中加载属性文件?

java - 如何将字符串转换为时间并将其插入MySQL

java - 对象不会更新

使用 Block 实例化 Java 类

Java 1.6 java.lang.IndexOutOfBoundsException 问题

scala - 创作 future 与尝试

ruby - Ruby : Should we really believe the style guide? 中的失败与提升

java - 在 Spring MVC 中不使用 @Valid 验证 POJO