如何解决异常 java.lang.ClassCastException:java.math.BigDecimal 无法转换为 [Ljava.lang.Object;在 java
How to resolve exception java.lang.ClassCastException: java.math.BigDecimal cannot be cast to [Ljava.lang.Object; in java
在我的代码中 query
工作正常,但在迭代时抛出此错误。
我的代码在这里:
Session session = null;
try{
Query qu=session.createSQLQuery("select plot_no from house_details where type='duplex'");
List<Long> li = qu.list();
System.out.println("---li---"+li.toString());
for (Iterator itr = li.iterator(); itr.hasNext();) {
String plotNo = itr.next().toString();
if(plotNo.equals("501") || plotNo.equals("520") || plotNo.equals("601"){
System.out.println("---if---");
//code here
}
else{
System.out.println("---else---");
Query qu1 = session.createSQLQuery("select distinct name,houseno from house_details");
List li1 = qu1.list();
for (Iterator itr1 = li.iterator(); itr1.hasNext();) {
Object[] obj = (Object[]) itr1.next();
String houseName = (String) obj[0];
String houseNo = (String) obj[1];
System.out.println("---houseName--->"+houseName);
}
}
}catch(Exception e){
e.printStackTrace();
}finally {
if(session!=null){
session.close();
}
}
输出:
---li---[501, 0, 101, 101, 114]
---else---
java.lang.ClassCastException: java.math.BigDecimal cannot be cast to [Ljava.lang.Object;
它在 Object[] obj = (Object[]) itr1.next();
行显示错误。
我的代码有什么问题?
如错误所述:无法将单个 BigDecimal 转换为对象数组。
您有错误的列表要迭代:li <> li1。
将 for (Iterator itr1 = li.iterator(); itr1.hasNext();) {
更改为 (Iterator itr1 = li1.iterator(); itr1.hasNext();) {
然后它应该可以工作。
首先,转换:您将列表 li
上的迭代器视为数组列表上的迭代器,而实际上它是 [=14= 列表上的迭代器]s。您应该迭代 li1
:
for (Iterator itr1 = li1.iterator() ; itr1.hasNext() ; )
// ^
早期发生另一个错误:
List<Long> li = qu.list();
应该是
List<BigDecimal> li = qu.list();
因为查询返回的列表中的对象属于 BigDecimal
类型,已通过失败的转换确认。
在我的代码中 query
工作正常,但在迭代时抛出此错误。
我的代码在这里:
Session session = null;
try{
Query qu=session.createSQLQuery("select plot_no from house_details where type='duplex'");
List<Long> li = qu.list();
System.out.println("---li---"+li.toString());
for (Iterator itr = li.iterator(); itr.hasNext();) {
String plotNo = itr.next().toString();
if(plotNo.equals("501") || plotNo.equals("520") || plotNo.equals("601"){
System.out.println("---if---");
//code here
}
else{
System.out.println("---else---");
Query qu1 = session.createSQLQuery("select distinct name,houseno from house_details");
List li1 = qu1.list();
for (Iterator itr1 = li.iterator(); itr1.hasNext();) {
Object[] obj = (Object[]) itr1.next();
String houseName = (String) obj[0];
String houseNo = (String) obj[1];
System.out.println("---houseName--->"+houseName);
}
}
}catch(Exception e){
e.printStackTrace();
}finally {
if(session!=null){
session.close();
}
}
输出:
---li---[501, 0, 101, 101, 114]
---else---
java.lang.ClassCastException: java.math.BigDecimal cannot be cast to [Ljava.lang.Object;
它在 Object[] obj = (Object[]) itr1.next();
行显示错误。
我的代码有什么问题?
如错误所述:无法将单个 BigDecimal 转换为对象数组。
您有错误的列表要迭代:li <> li1。
将 for (Iterator itr1 = li.iterator(); itr1.hasNext();) {
更改为 (Iterator itr1 = li1.iterator(); itr1.hasNext();) {
然后它应该可以工作。
首先,转换:您将列表 li
上的迭代器视为数组列表上的迭代器,而实际上它是 [=14= 列表上的迭代器]s。您应该迭代 li1
:
for (Iterator itr1 = li1.iterator() ; itr1.hasNext() ; )
// ^
早期发生另一个错误:
List<Long> li = qu.list();
应该是
List<BigDecimal> li = qu.list();
因为查询返回的列表中的对象属于 BigDecimal
类型,已通过失败的转换确认。