gpt4 book ai didi

java - 设置名称时抛出异常

转载 作者:行者123 更新时间:2023-12-01 12:39:20 24 4
gpt4 key购买 nike

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

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

异常(exception)情况是:

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

最佳答案

那么没有人愿意去阅读异常消息吗?

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

前导的[表示该类是一个数组类。

您在上面得到的是:

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

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com