Shawn Williams Shawn Williams - 1 year ago 48
Java Question

ClassCastException at OIS

Im getting a CCE when im reading an object in from a bat file.

loader class:

public static void loader()throws IOException, ClassNotFoundException{
FileInputStream fis = new FileInputStream("students.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
try {
stud = ois.readObject();
student = (Student) stud;
}catch(EOFException e){
}catch(FileNotFoundException e) {
System.out.println("File not found");

The CCE error im getting is at the line: student = (Student) stud;

The specific error code im getting is
Caused by: java.lang.ClassCastException: [LBags.Student; cannot be cast to Bags.Student

Im also not sure where its getting LBags,student from... i dont have any package or class or anything named LBags

Answer Source

The string [LBags.Student is an internal type signature for Array of Bags.Student. The first character [ indicates an array, and the L indicates a reference type.

So, the message is saying you are trying to cast an array of Bags.Student to a Bags.Student, which is clearly not possible. Thus, your serialized data contains an array, not a scalar object.

A complete list of type signatures is available in the JNI Documentation. For completeness here is the list copied from that document:

Type Signature

Z                          boolean
B                          byte
C                          char
S                          short
I                          int
J                          long
F                          float
D                          double
Lfully-qualified-class;    object of class
[type                      Array of type