Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing throws java.lang.ClassCastException

I save a serialised class on an android device. Transfer it to a win 10 PC and load the file via:

fis = new FileInputStream(result.get(i));
ois = new ObjectInputStream(fis);
Object obj = ois.readObject();

The class on Android and an win 10 is:

public class ImgLogFile implements Serializable{

        byte[] frame;
        byte[] result;
        String config;

    public String getConfig(){
            return config;
        }

    public byte[] getFrame() {
        return frame;
    }

    public byte[] getResult() {
        return result;
    }
} 

When i try to cast the loaded Object into it's class i get:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class [Lde.mtt.smartiePlatform.ImgLogFile; cannot be cast to class de.mtt.smartiePlatform.ImgLogFile ([Lde.mtt.smartiePlatform.ImgLogFile; and de.mtt.smartiePlatform.ImgLogFile are in unnamed module of loader 'app')

I noticed the "L" in front of one path but do not know what it means. How can I fix this ?

like image 247
Niro Avatar asked Oct 26 '25 03:10

Niro


1 Answers

[...] class [Lde.[...] cannot be cast to class de.[...]

The [ indicates an array. [L is an array of the reference type that follows.

So you have serialised an array and are attempting to cast the deserialised object to a type that is not an array.

(Also, it's not a great idea to use Java Serialization.)

like image 120
Tom Hawtin - tackline Avatar answered Oct 28 '25 17:10

Tom Hawtin - tackline



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!