Java错误 [英] Java Error

查看:48
本文介绍了Java错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



运行代码时出现错误.

我反序列化了其中没有几个对象的列表,并试图访问这些对象;

Hi,

I am getting an error while running my code,.

I deserialised a list with few objects in it and am trying to access these objects;

Iterator iterate = list.iterator();
    while(iterate.hasNext()) {
        Student student = (Student) iterate.next();
        if(a == (int)student.roll_no) {
            try {
                System.out.println("Re-enter your roll no. :  ");
                a = Integer.parseInt(br.readLine());
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }


但是它不能使用另一个int值来转换简单的int"rollno"字段;

我收到以下异常


But it is not able to cast the simple int "rollno" field with another int value;

I am getting the following Exception

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to com.Student


问候
Nakul Kundra


Regards
Nakul Kundra

推荐答案

错误是因为您试图将Integer强制转换为Student.
iterate包含Integer的列表,或者a被定义为Student.
The error is because you are trying to cast an Integer to a Student.
Either iterate contains a list of Integers or a is defined as a Student.


这篇关于Java错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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