如何解决 java.lang.ClassCastException: java.math.BigDecimal 无法转换为 [Ljava.lang.Object; 在 Java 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36930978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:50:36  来源:igfitidea点击:

how to solve java.lang.ClassCastException: java.math.BigDecimal cannot be cast to [Ljava.lang.Object; in java

java

提问by Sri

I'm getting values through query and next i'm trying to iterating values but is throws error

我通过查询获取值,接下来我尝试迭代值但抛出错误

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();) {
        Object[] obj = (Object[]) itr.next();
        String plotNo = (String) obj[0];
        if(plotNo=="501" || plotNo== "520" || plotNo== "601"){
        System.out.println("---if---");
        //code here
        }
        else{
        System.out.println("---else---");
        //code here
        }
    }
 }catch(Exception e){
       e.printStackTrace();

   }finally {
       if(session!=null){
        session.close();
     }
    }

output:

输出:

---li---[501, 0, 101, 101, 114]
java.lang.ClassCastException: java.math.BigDecimal cannot be cast to [Ljava.lang.Object;

what is wrong in my code. Error getting this lines

我的代码有什么问题。获取此行时出错

Object[] obj = (Object[]) itr.next();
String plotNo = (String) obj[0];

采纳答案by Jesper

The call to itr.nextreturns a BigDecimal, not an Object[]. You are trying to cast it to an Object[]:

调用itr.next返回 a BigDecimal,而不是 an Object[]。您正在尝试将其强制转换为Object[]

Object[] obj = (Object[]) itr.next();
String plotNo = (String) obj[0];

You get a ClassCastExceptionbecause it is not an Object[].

你得到 aClassCastException因为它不是Object[]

Replace the two lines above with this one line:

用这一行替换上面的两行:

String plotNo = itr.next().toString();

回答by mahesh

try to use

尝试使用

String testName = obj[0].toString();

Regards Mahesh

问候马赫什

回答by Alex

Object[] obj = (Object[]) itr.next();
String testName = (String) obj[0];

You expect that java understands what you want to do. Frequently it will be true, and it is described in documentation. Actually, method toString() is called when another method obtains string as arg. In your case you expect something similar on unboxing and boxing( it is only true for Object class). And method toString() won't be called.

您希望 java 了解您想要做什么。通常它会是真的,并且在文档中进行了描述。实际上,当另一个方法获取字符串作为 arg 时,会调用方法 toString()。在您的情况下,您希望在拆箱和装箱方面有类似的东西(仅适用于 Object 类)。方法 toString() 不会被调用。

try to use method toString() explicitly which exist in class Object and all classes inherit this. It is made special for this case.

尝试显式使用存在于类 Object 中的方法 toString() 并且所有类都继承了它。它是专为这种情况而设计的。

String testName = obj[0].toString();