设置名称上的类强制转换异常

时间:2014-09-23 11:52:38

标签: java

我在设置名称时遇到强制转换异常。

    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

2 个答案:

答案 0 :(得分:0)

嗯,根据例外情况,存储在数组中的对象不是String类型。

您可以使用toString()方法:

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

答案 1 :(得分:-1)

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