Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
C#
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();
            }
        }

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

I am getting the following Exception
C#
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to com.Student

Regards
Nakul Kundra
Posted
Updated 22-Oct-10 1:24am
v3
Comments
Dalek Dave 22-Oct-10 6:52am    
Edited for Grammar.
GPUToaster™ 22-Oct-10 8:28am    
Does your list contain same type of objects? If yes then make sure you cast the correct object to type Student.
Also it is a good practice to use Generics to avoid this confusion.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900