gpt4 book ai didi

java - 从迭代器的 next 方法中获取 ClassCastException

转载 作者:行者123 更新时间:2023-12-02 06:51:12 26 4
gpt4 key购买 nike

我在 Tomcat 7 上使用 Spring 3.1.0、Hibernate 4、JDK 7,并在 itr.next() 方法上收到 ClassCastException。 aaData 对象确实包含数据。

List<CustomerList> aaData = customerlistDaoimpl.getCustomerList();

/*
* putting data in a JSON form that DataTables recognizes
*/
String data = "{\"sEcho\": 3, \"iTotalRecords\": " + count + ",\"iTotalDisplayRecords\": " + count + ",\"aaData\": [ ";
Iterator<CustomerList> itr = aaData.iterator();
while(itr.hasNext()){
CustomerList cl = (CustomerList) itr.next();
data += "[\"" + cl.getName() + "\",\"" + cl.getAddress() + "\",\"" + cl.getZipcode() + "\",\"" +
cl.getPhone() + "\",\"" + cl.getCity() + "\",\"" + cl.getCountry() + "\",\"" + cl.getNote() + "\" ] ";
count++;
}
data += "]}";

我的道

@SuppressWarnings("unchecked")
@Override
public List<CustomerList> getCustomerList() {
List<CustomerList> cuList = null;
Session session = null;

try{
session = sessionfactory.openSession();
session.beginTransaction();
cuList = session.createSQLQuery("select * from customer_list").list();
session.getTransaction().commit();
}catch (RuntimeException e){
System.out.println(e.getMessage());
}
finally
{
if(session != null){
session.close();
}
}

return cuList;
}

以及回溯

SEVERE: Servlet.service() for servlet [sptestjs] in context with path [/SPTestJs] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.sptestjs.implementation.CustomerList] with root cause java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.sptestjs.implementation.CustomerList at com.sptestjs.implementation.controller.HomeController.getCustomerList(HomeController.java:85) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

我确实找到了电话

SQLQuery cuSQLQuery = session.createSQLQuery("select * from customer_list");

返回一个 SQLQuery 实例,其列表是 Object 元素的 ArrayList 类型,其中

Query cuQuery = session.createQuery("from customer_list");

返回null

最佳答案

Getting a ClassCastException from my iterator next method

这意味着您aaData实际上不是List<CustomerList>您在某处进行了类型删除,并且错误地更改了它的类型。如果您仔细查看 ClassCastException,它会告诉您组件的真正类型。

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.sptestjs.implementation.CustomerList

这表明该类型实际上应该是

List<Object[]> cuList = session.createSQLQuery("select * from customer_list").list(); 

关于java - 从迭代器的 next 方法中获取 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18009663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com