2011-01-25 32 views
0

我想問一下,java.lang.ClassCastException問題

爲什麼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); 

        } 
       } 
      } 
     } 
    */ 
%> 
+1

哪裏回溯?在哪個行上觸發異常? – Satya 2011-01-25 15:42:00

+0

商店=(商品)it.next(); 這條線有問題 無論如何,我已經解決了這個問題,謝謝 – user540719 2011-01-25 15:50:36

回答

1

這是犯罪嫌疑人

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

你確定你已經在會議

+0

恩......我明白了你的觀點......你的建議很有用,謝謝! – user540719 2011-01-25 15:48:54

2

集汽車作爲ArrayList中,您需要使用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"); 
相關問題