3

I create class with methods like a How to serialize an object into a string and it every say error "java.lang.ClassCastException: java.lang.String cannot be cast to Myclass"

My codes: 1)

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import javax.xml.bind.DatatypeConverter;

public class Serialization {

    public static Object fromString(String s) throws IOException,
            ClassNotFoundException {
        byte[] data = DatatypeConverter.parseBase64Binary(s);
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
                data));
        Object o = ois.readObject();
        ois.close();
        return o;
    }

    public static String toString(Serializable o) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(o);
        oos.close();
        return DatatypeConverter.printBase64Binary(baos.toByteArray());
    }
}

2) - calling

MyClass hl = (MyClass) Serialization.fromString(items
        .getString("data"));
hl.load(); // this is my method from class

3) MyClass - Hologram

public class Hologram implements Serializable {

 /**
 * 
 */
private static final long serialVersionUID = 1L;

private Location loc;
private String name;
private String displayname;
public ArmorStand stand;

public Hologram(String name, String displayname, Location loc) {
this.loc = loc;
this.name = name;
this.displayname = displayname;

ArmorStand as = (ArmorStand) loc.getWorld().spawnEntity(loc,
    EntityType.ARMOR_STAND);

as.setGravity(false);
as.setCanPickupItems(false);
as.setCustomName(displayname);
as.setCustomNameVisible(true);
as.setVisible(false);

this.stand = as;

HologramManager.holograms.put(name, this);

}

public void move(Location loc) {
this.loc = loc;

stand.teleport(loc);
}

public Location getLocation() {
return this.loc;
}

public void remove() {
stand.remove();
HologramManager.holograms.remove(name);
}

public void removeHologram() {
HologramManager.remove(name);
}

public void changeName(String name) {
HologramManager.holograms.remove(this.name);
this.name = name;
HologramManager.holograms.put(name, this);
}

public void changeDisplayName(String displayName) {
this.displayname = displayName;
stand.setCustomName(displayname);
stand.setCustomNameVisible(true);
}

public void load() {
//todo
}

}
Community
  • 1
  • 1
DenOwq DO
  • 33
  • 6

2 Answers2

1

Based on the linked answer, the problem most likely lies in the code you aren't showing us. When you serialize your MyClass object, you are probably doing something like this:

MyClass hl;
String base64String = Serialization.toString(hl.toString());

However you should be calling it like this:

MyClass hl;
String base64String = Serialization.toString(hl);

If you pass a String to the serialization function, you'll get a String back when you call Serialization.fromString(). You want to get an object back that you can cast to a MyClass instance, so pass one of those into Serialization.toString().

samgak
  • 23,944
  • 4
  • 60
  • 82
0

The fromString() method in Serilization returns an Object, which you wouldnt be able to cast to the class MyClass. The below line is causing the classCastException

MyClass hl = (MyClass) Serialization.fromString(items
    .getString("data"));
Kalyan Chavali
  • 1,330
  • 8
  • 24