This question already has answers here:
Cast exception while setting name
                                
                                    (2个答案)
                                
                        
                                5年前关闭。
            
                    
设置名称时出现强制转换异常。

        Object[] customers= customerRepository.getCustomerName(Id);
        Customer row = new Customer();
        row.setName((String) customers[0]+" "+(String) customers[1]);


例外是:

HTTP Status 500 - Request processing failed;
nested exception is java.lang.ClassCastException:
[Ljava.lang.Object; cannot be cast to java.lang.String

最佳答案

您获得的是:

  row.setName(((String) customers)[0]+" "+((String) customers)[1]);


强制转换优先于数组索引。

线索是消息[Ljava.lang.Object; cannot be cast to java.lang.String

前导[表示该类是数组类。

关于java - 设置名称时出现类强制转换异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25269382/

10-12 04:11