java.lang.ClassCastException:java.lang.String无法强制转换为components.Student [英] java.lang.ClassCastException: java.lang.String cannot be cast to components.Student

查看:86
本文介绍了java.lang.ClassCastException:java.lang.String无法强制转换为components.Student的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

我有问题,我的java代码出错了,我不确定为什么会这样。我想要做的是打印每个学生的城市。学生是一个对象,作为一个功能是城市。如果被调用,City将返回一个字符串。每个学生对象都存储在一个名为full的arrayList中。

这是我的代码:



Hey,
I have a problem, I get an error in my java code, I'm not sure to understand why it is there. What I am trying to do is to print the city of each student. student is an object, which as one function which is city. City will return a string if called. Each student object is stored in an arrayList called full.
Here is my code:

for (int i = 0; i < full.size(); i++){
            System.out.println(full.get(i).city().toString());
        }





,错误如下:





and the error is the following:

Exception in thread "Thread-0" java.lang.ClassCastException: java.lang.String cannot be cast to components.Student
	at components.ClientThread.createTable(ClientThread.java:202)
	at components.ClientThread.run(ClientThread.java:88)

推荐答案

好吧,好像你是在那里返回一个对象。



Heave你调试了吗?



试试这个:



Well, seems like you're returning an Object there.

Heave you debugged it?

Try this instead:

for (int i = 0; i < full.size(); i++)
{
  City city = full.get(i).city();
  Student student = city.getStudent(); // or what ever that is
  System.out.println("City: " + city.toString() + "\tStudent: " + student.toString()); // if you have set the toString(), otherwise a String representing the object is used.
}


这篇关于java.lang.ClassCastException:java.lang.String无法强制转换为components.Student的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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