Wednesday, March 23, 2011

Unknown exception while deserializing using simple XML

I am deserializing data using Simple XML in Java, but i get an exception telling me:

protokolsimulering.model.Terminal.<init>()

This is my serializing code:

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());
    }
}

The code for Terminal.java is:

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()));
        }
    }
}

}

The entire source code can be found at:

http://code.google.com/p/sensor-protocol-simulation/source/browse/#svn/trunk/src/protokolsimulering

From stackoverflow
  • The exception means the problem is related to invoking the Terminal constructor.

    Typically, to construct objects when deserializing, a no-args contructor must be available, like this:

    public Terminal() {
        ...
    }
    
    erickson : Yes, while the OP's question is very vague and leaves out most useful information, it's clear that something is looking for a no-arg constructor for Terminal.
    utdiscant : I fixed this by adding a constructor to Terminal, but now the error seems to be: protokolsimulering.model.DataPacket$HalloPacket.() Which i tried to solve using the same method, only this did not do the trick.
    Jordan Liggitt : Without more details, it's hard to say, but you might make sure you added an empty constructor to HalloPacket (the error is on an inner class, not on DataPacket)
    utdiscant : I added the empty constructor to all classes related to DataPacket (also HalloPacket). I can post all needed details here, just not sure what would be relevant. I have posted a link to the complete source code and the exception i am getting.

0 comments:

Post a Comment