日期:2014-05-18  浏览次数:20780 次

急救:java.lang.String cannot be cast to [Ljava.lang.Obj
使用hibernate3.1,strut1.2,数据库用mssqlserver2000
  在执行 String hql=" FROM Oauser as a " ;
Vector myList1=new Vector();
ArrayList result1 = new ArrayList();

Transaction t = null; 
t = this.session.beginTransaction(); 
Query q = this.session.createQuery(hql); 
result1 = (ArrayList) q.list(); 
t.commit(); 

Iterator myIterator1= result1.iterator(); 
Oauser oauser=new Oauser();
while (myIterator1.hasNext()) 

Object[] o = (Object[]) myIterator1.next();
oauser=(Oauser) o[0];
System.out.println(oauser.getPoliceid());
}
  后,报错:

java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Obj
ect;
  at common.db.dboperation.DbOperation.sendMessages(DbOperation.java:494)
  at org.apache.jsp.oa.oapad.send_jsp._jspService(send_jsp.java:228)
  at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:384)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:3
20)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:290)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:228)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:175)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:128)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:104)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:216)
  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:844)
  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ss(Http11Protocol.java:634)
  at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:44
5)
  at java.lang.Thread.run(Thread.java:619)


  困扰我几天了。。。 但是前面有个查询,可以照常使用,没有提示字符类型不匹配问题。
  急救!!


------解决方案--------------------
while (myIterator1.hasNext()) 

//Object[] o = (Object[]) myIterator1.next();
oauser=(Oauser) myIterator1.next();
System.out.println(oauser.getPoliceid()); 

还有前面直接用List接口就可以了
没有必要转为ArrayList

试下把。
------解决方案--------------------
Object[] o = (Object[]) myIterator1.next(); 

myIterator1.next不是对象数组,肯定不能用对象数组转换类型
直接就可以了
oauser=(Oauser) myIterator1.next();
------解决方案--------------------
Object[] o = (Object[]) myIterator1.next(); 
oauser=(Oauser) o[0]; 

按你的思路改成这样应该也可以
Object o = myIterator1.next(); 
oauser=(Oauser) o; 


------解决方案--------------------