设置名称时抛出异常 [英] Cast exception while setting name

查看:84
本文介绍了设置名称时抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am getting a cast exception while setting the name.

        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


推荐答案

所以没有一个人打扰阅读异常消息?

So no one bothered to read the exception message?

[Ljava.lang.Object; cannot be cast to java.lang.String

领先 [表示该类是数组类。

The leading [ indicates that the class is an array class.

您获得的是:

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

Cast优先于数组索引。

Cast has precedence over array indexing.

这篇关于设置名称时抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆