gpt4 book ai didi

java - 使用简单 XML 反序列化时出现未知异常

转载 作者:行者123 更新时间:2023-12-01 16:12:41 27 4
gpt4 key购买 nike

我正在 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

最佳答案

该异常意味着问题与调用终端构造函数有关。

通常,要在反序列化时构造对象,必须提供无参数构造函数,如下所示:

public Terminal() {
...
}

关于java - 使用简单 XML 反序列化时出现未知异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/441272/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com