Question:
java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;?
?
2014-05-14 04:46:39 UTC
public List getRecordList(){
List listobj=new ArrayList();
Statement st=con.createStatement();
st.setMaxRows(10);
String sql="select * from record";
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
id=rs.getString(1);
name=rs.getString(2);
regno=rs.getString(3);
listobj.add(id);
listobj.add(name);
listobj.add(regno);
}
return listobj;
}


public String showRecords()
{
List detail = getRecordList()
{
for (Object[] Details : detail) {
Reg reg= new Reg();
if(Details!=null)
{
if(Details[0]!=null){
reg.setId( (new Long(id)));
}
} }

return SUCESS;
}

I am getting exception:
java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;

on these line:
List detail = getRecordList()

please advice.
Three answers:
howells
2016-12-30 01:47:06 UTC
Classcastexception Java
KRANTI
2014-05-14 06:26:56 UTC
Basically, you are getting an object of type List and trying to convert it into List instead. This won't work. When you're doing listObj.add(), it is adding an object to the list. This is more like a 1-D DS.



You need to create a class like

class DataBaseClass {

Sting id, regno, name;

public DataBaseClass(id, regno, name){// set fields to these values

}

//define getter and setters

}

create an DataBaseClass object and add it to the list.

DataBaseClass object = new DataBaseClass (rs.getString(1),rs.getString(3),rs.getString(2));

listObj.add(object);



Then iterate over this object to print records.



Let me know if that helped.
?
2016-11-14 11:50:09 UTC
Ljava.lang.object


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...