exception - java.lang.ClassCastException problem -


i want ask,

why java.lang.classcastexception triggered in program ??

i not sure reason this,

could mind giving advice me?

thanks 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 = ay.iterator();                  //object shop1 = it.next();                   for(int = 0;i<ay.size();i++)                   {                     //this statement triggers java.lang.classcastexception                     //i not sure 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);                      }                 }             }         }     */ %> 

you need use iterator<goods>.

iterator<goods> = ay.iterator(); 

also, can use foreach loop instead of using iterator. functionally, it's same thing, it's lot cleaner semantically.

for (goods g in ay) {     // stuff } 

lastly, think

ay=(arraylist)session.getattribute("car"); 

should be

ay = (arraylist<goods>)session.getattribute("car"); 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -