Hi guys I am getting this problem on my console, but only appear sometimes.. not always..
I would like your help if possible, thanks
Error:
Exception in thread "Thread-2" java.lang.ClassCastException: cannot assign instance of java.lang.String to field Element.posElement of type java.awt.Point in instance of Personagem
at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(Unknown Source)
at java.io.ObjectStreamClass.setObjFieldValues(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at LaunchCliente$receberDoServidor.run(LaunchCliente.java:332)
and here is my code, when I am trying to read the object :
Object a = inputStream.readObject();
if(a instanceof Mapa){
Mapa novo = (Mapa) a;
if(launchJogo.getListaObstaculos().size() == 0)
launchJogo.setLista(novo.getListaObstaculos());
launchJogo.setListaPers(novo.getListaPersonagens());
launchJogo.setElements(novo.getElements());
launchJogo.getFrame().pack();
}
else if(a instanceof logout){
if(launchJogo != null)
launchJogo.getFrame().dispose();
novo.close();
}
else if(a instanceof updateList){
Vector<String> novo = ((updateList) a).getUpdateList();
if(novo.size() != 0){
if(!nomeUtilizador.isEnabled()){
modeloDaLista.clear();
arrayDeJogos = new Vector<String>(novo);
for (String x : arrayDeJogos) {
modeloDaLista.addElement(x);
}
janela.validate();
}
}
if(novo.size() == 0){
modeloDaLista.clear();
}
}
else if(a instanceof String){
String b = a.toString();
if(b.equals("COLOR:FALSE")){
JOptionPane.showMessageDialog(c, "Essa cor já está em uso no jogo selecionado!" , "Cor já escolhida!",
JOptionPane.WARNING_MESSAGE);
launchJogo = null;
cores.dispose();
janela.setVisible(true);
}
else if(b.equals("CREATE:BUTTONSTART")){
launchJogo.getOptions().showButton(true);
}
else if(b.equals("ACTIVE:BUTTONSTART")){
if(!launchJogo.getOptions().isButtonEnabled()){
launchJogo.getOptions().setBotaoState(true);
}
}
else if(b.equals("COLOR:TRUE")){
cores.dispose();
out.writeObject(new addToAGame(corDoJogador, nomeJogador, jogoSelecionado));
launchJogo = new LaunchJogo(launchCliente, jogoSelecionado, nomeJogador);
}
else if(b.equals("LAUNCH:GAME")){
if(launchJogo != null)
launchJogo.addPersonagemListener();
}
}
} catch (ClassNotFoundException e) {
System.out.println("Class not found!");
} catch (IOException e) {
this.interrupt();
}
Thanks alot in advance guys, I would appreciate some help
The error message is telling you that the object stream is encountering a runtime error while rebuilding a serialized object. The object state being deserialized has a
Stringfor a value of the fieldElement.posElement… which ought to be aPointaccording to the version of theElementclass that your application is using.The problem is with the actual serialized objects that you are attempting to read. I suspect that some time in the past you have changed the type of the
Element.posElementfield fromStringtoPoint(or vice versa), and you are trying to deserialize an object created by the other version of the class to the one you are currently using.Normally, the object reader would complain about incompatible versions, but I suspect that you have:
– added
serialVersionIdconstants to your classes, and– failed to update the
serialVersionIdforElementafter making a change that makes the new version incompatible with the old one.Another possibility (see @TInusTate’s comment) is that there are multiple threads writing to a shared
ObjectOutputStreaminstance without synchronizing properly. This can result in the bytes from different objects getting “mixed up”.You could get a similar effect if you did other things like:
ObjectOutputStreaminstances wrapping a sharedOutputStream.ObjectInputStream.In some cases, the solution is proper synchronization. In others, the problem is intractable. None of the stream classes are inherently thread-safe, and multiple threads multiplexing data over a single stream is a difficult problem, requiring considerable care.
You are going to need to find where these incompatible serialized objects are coming from and either get rid of them or (somehow) replace them. In the future, you need to be more careful whan making changes to classes that may have been serialized and persisted, etcetera.
(This kind of problem is one of the reasons why ObjectStream serialization can be problematic for persisting state.)