我使用Java中的Simple XML反序列化数据,但我得到一个例外告诉我:
protokolsimulering.model.Terminal.<init>()
这是我的序列化代码:
public void saveSimulationState(String simulationFile) {
try{
Strategy strategy = new CycleStrategy("id", "ref");
Serializer serializer = new Persister(strategy);
File result = new File(simulationFile);
serializer.write(this.sm, result);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public void loadSimulationState(String simulationFile) {
try {
Strategy strategy = new CycleStrategy("id", "ref");
Serializer serializer = new Persister(strategy);
File source = new File(simulationFile);
this.sm = serializer.read(Simulation.class, source);
} catch (Exception ex) {
System.out.println(ex.getLocalizedMessage());
}
}
Terminal.java的代码是:
package protokolsimulering.model;
import java.util.ArrayList;
import protokolsimulering.model.DataPacket.*;
public class Terminal extends Sensor {
public Terminal(int x, int y, double r) {
super(x,y,r);
learnPosition();
}
@Override
public void init() {
this.broadcast(PacketFactory.newRutePacket(this,0));
}
@Override
public void step() {
ArrayList<AbstractPacket> packetsThisStep = (ArrayList<AbstractPacket>) this.getPackages();
for(AbstractPacket p : packetsThisStep) {
if(p instanceof HalloPacket) {
// Rutningsprotokol
send(p.getSender(),PacketFactory.newRutePacket(this,0));
// Lokaliseringsprotokol
send(p.getSender(), PacketFactory.newHerPacket(this, this.getKnownPosition()));
}
}
}
}
整个源代码可在以下网址找到:
http://code.google.com/p/sensor-protocol-simulation/source/browse/#svn/trunk/src/protokolsimulering
答案 0 :(得分:1)
异常意味着问题与调用Terminal构造函数有关。
通常,要在反序列化时构造对象,必须使用no-args构造函数,如下所示:
public Terminal() {
...
}