This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kartoffelbrot
Kartoffelbrot wrote ...

2013/10/14

Serialization

1
2
Kartoffelbrot Kartoffelbrot

2013/10/14

#
Hello, I get this error java.io.InvalidClassException: Editor; no valid constructor ,when I want to serialize my world (Editor). I have a default constructor there, but do I get this error because of the super statement?
danpost danpost

2013/10/14

#
You will have to show what code you are using and the constructors for the class you are trying to instantiate an object from.
Kartoffelbrot Kartoffelbrot

2013/10/14

#
import greenfoot.*;
import java.util.*;
import java.awt.Color;
import java.io.Serializable;
public class Editor extends World implements Serializable
{
//stuff
//another constructor

public Editor(){
        super(800, 500, 1, false);

        logicHazard = true;

        firstChoosen = null;
        secondChoosen = null;
        selected = new Part[0];

        marker = new Image(1,1);

        edit = new EditWindow(this);
        addObject(edit, (edit.getWidth()/2)-1, getHeight()/2);
        edit.prepare();
        edit.loadPage(0);

        saveButton = new GButton(100,40);
        saveButton.setText("save");
        addObject(saveButton, getWidth()-saveButton.getWidth()/2, saveButton.getHeight()/2);

        //updating the world and its objects
        update();
        paintOrder();
        Greenfoot.start();
    }

public void SAVE(){
        Greenfoot.setWorld(new FileLoad(this));
    }

//much more stuff
}
This is the code of the FileLoad class. It a world, too, and there is so much code because I had to implement a GUI and it is used to Display files, too. GLabel, GTextField and GLabel are smaller sisterclasses of JLabel, JButton etc. for Greenfoot.
import greenfoot.*;
import java.util.*;
import java.awt.Color;
/**
 * displays the saved files for loading or saving
 */
public class FileLoad extends World
{
    private final boolean isLoad;
    private Editor home;
    private Editor[] files;
    private Save save;
    private Editor selected;
    private GLabel[] filesDisp;
    private GTextField fileNamer;
    private GTickBox[] filesDispSelection;
    private GTickBox ticked;
    private GButton load, up, down, delete, back;
    private int scrollSpeed = 5;

    public FileLoad()
    {    
        super(800, 500, 1);
        initializeGUI(true);
        home = null;
        isLoad = true;
        fileNamer = null;
        ticked = null;
    }

    /**
     * this constructor is used for saving files
     */
    public FileLoad(Editor edit){
        super(800, 500, 1);
        if(edit==null)
            throw new IllegalArgumentException("edit can't be null");
        home = edit;
        initializeGUI(false);
        isLoad = false;
        if(filesDisp!=null)
            if(filesDisp.length>0)
                scroll(0, filesDisp[0].getHeight());
        fileNamer = new GTextField(400, 50);
        fileNamer.setText(home.getName());
        addObject(fileNamer, fileNamer.getWidth()/2, fileNamer.getHeight()/2+10);
        if(filesDisp!=null){
            GTickBox[] t = new GTickBox[filesDispSelection.length+1];
            for(int i = 1; i <= filesDispSelection.length; i++)
                t[i] = filesDispSelection[i-1];
            t[0] = new GTickBox(50,50);
            filesDispSelection = t;
            if(filesDisp.length>0)
                addObject(t[0], fileNamer.getX()+fileNamer.getWidth()/2+t[0].getWidth()/2,
                    fileNamer.getY());
        }
        ticked = null;
    }

    private void initializeGUI(boolean isload){
        up = new GButton(50,getHeight()/2-25);
        down = new GButton(50, up.getHeight());
        up.setText(CreateACircuit.and+"");
        down.setText(CreateACircuit.or+"");
        addObject(up, getWidth()-up.getWidth()/2, up.getHeight()/2);
        addObject(down, getWidth()-down.getWidth()/2, up.getHeight()+ down.getHeight()/2);

        load = new GButton(120, 50);
        if(isload)
            load.setText("Load");
        else
            load.setText("Save");
        addObject(load, getWidth()-load.getWidth()/2, getHeight()-load.getHeight()/2);

        delete = new GButton(120, 50);
        delete.setText("Delete");
        addObject(delete, getWidth()-delete.getWidth()/2-load.getWidth(),
            getHeight()-delete.getHeight()/2);
        back = new GButton(80, 40);
        back.setText("<-");
        addObject(back, back.getWidth()/2, getHeight()-back.getHeight()/2);

        loadFiles();
        displayFiles();

        selected = null;

        if(files==null){
            GLabel i = new GLabel(500, 200);
            i.setText("No files found");
            i.setBackground(Color.WHITE);
            addObject(i, getWidth()/2, getHeight()/2);
        }else if(files.length==0){
            GLabel i = new GLabel(500, 150);
            i.setText("No files found");
            i.setBackground(Color.WHITE);
            addObject(i, getWidth()/2, getHeight()/2);
        }
    }

    public void act(){
        if(isLoad){
            if(filesDisp!=null){
                if(filesDisp.length>0){
                    if(Greenfoot.isKeyDown("up")||up.isHold())
                        if(filesDisp[0].getY()-filesDisp[0].getHeight()/2<10)
                            scroll(0,-scrollSpeed);
                    if(Greenfoot.isKeyDown("down")||down.isHold())
                        if(filesDisp[filesDisp.length-1].getY()+
                        filesDisp[filesDisp.length-1].getHeight()/2>getHeight()-10)
                            scroll(0,scrollSpeed);
                }
                for(int i = 0; i < filesDispSelection.length; i++){
                    GTickBox t = filesDispSelection[i];
                    if(Greenfoot.mouseClicked(t)){
                        selected = files[i];
                        for(int j = 0; j < filesDispSelection.length; j++)
                            if(i!=j)
                                filesDispSelection[i].setTick(false);
                    }
                }
                if(load.isPressed()&&selected!=null){
                    SAVE();
                    Greenfoot.setWorld(selected);
                }else if(delete.isPressed()&&selected!=null){
                    save.deleteFile(selected);
                    SAVE();
                }
            }
        }else{
            if(filesDisp!=null){
                if(filesDisp.length>0){
                    if(Greenfoot.isKeyDown("up")||up.isHold())
                        if(filesDisp[0].getY()-filesDisp[0].getHeight()/2<10)
                            scroll(0,-scrollSpeed);
                    if(Greenfoot.isKeyDown("down")||down.isHold())
                        if(filesDisp[filesDisp.length-1].getY()+
                        filesDisp[filesDisp.length-1].getHeight()/2>getHeight()-10)
                            scroll(0,scrollSpeed);
                }
                for(int i = 0; i < filesDispSelection.length; i++){
                    GTickBox t = filesDispSelection[i];
                    if(Greenfoot.mouseClicked(t)){
                        ticked = t;
                        if(i==0)
                            selected = null;
                        else
                            selected = files[i];
                        for(int j = 0; j < filesDispSelection.length; j++)
                            if(i!=j)
                                filesDispSelection[i].setTick(false);
                    }
                }
            }
            if(load.isPressed()){
                if(save==null)
                    save = new Save();
                if(ticked==null&&fileNamer.getText()!=""){
                    home.setName(fileNamer.getText());
                    save.addFile(home);
                }else{
                    home.setName(selected.getName());
                    save.deleteFile(selected);
                    save.addFile(home);
                }
                SAVE();
                Greenfoot.setWorld(home);
                return;
            }else if(delete.isPressed()&&selected!=null){
                save.deleteFile(selected);
                SAVE();
            }
        }
        if(selected==null){
            if(isLoad)
                load.setEnabled(false);
            delete.setEnabled(false);
        }else{
            load.setEnabled(true);
            delete.setEnabled(true);
        }
        if(back.isPressed()){
            SAVE();
            if(isLoad)
                Greenfoot.setWorld(new Menu());
            else
                Greenfoot.setWorld(home);
        }
    }

    private void SAVE(){
        if(save==null)
            save = new Save();
        save.save();
    }

    private void loadFiles(){
        save = (Save) Serialization.deserialize(Save.getName());
        if(save!=null)
            files = save.getFiles();
        else
            files = null;
    }

    private void displayFiles(){
        if(files != null){
            filesDisp = new GLabel[files.length];
            filesDispSelection = new GTickBox[filesDisp.length];
            filesDisp[0] = new GLabel(300, 50);
            filesDisp[0].setText(files[0].getName());
            addObject(filesDisp[0], filesDisp[0].getWidth()/2, filesDisp[0].getHeight()/2+10);
            filesDispSelection[0] = new GTickBox(50, 50);
            addObject(filesDispSelection[0],
                filesDisp[0].getX()+filesDisp[0].getWidth()/2+filesDispSelection[0].getWidth()/2,
                filesDisp[0].getY());
            for(int i = 1; i < filesDisp.length; i++){
                filesDisp[i] = new GLabel(300, 50);
                filesDisp[i].setText(files[i].getName());
                addObject(filesDisp[i], filesDisp[i].getWidth()/2,
                    filesDisp[i-1].getY()+filesDisp[i].getHeight());
                filesDispSelection[i] = new GTickBox(50, 50);
                addObject(filesDispSelection[i],
                    filesDisp[i].getX()+filesDisp[i].getWidth()/2+filesDispSelection[i].getWidth()/2,
                    filesDisp[i].getY());
            }
        }else{
            filesDisp = null;
            filesDispSelection = null;
        }
    }

    /**
     * scrolls the world by the given distance, by moving all Actors
     */
    public void scroll(int dx, int dy){
        List<Superclass>things = getObjects(Superclass.class);
        for(Superclass a : things)
            if(a instanceof Interface){
                if(a instanceof GLabel||a instanceof GTickBox)
                    a.setLocation(a.getX()-dx, a.getY()-dy);
            }else
                a.setLocation(a.getX()-dx, a.getY()-dy);
    }
}
danpost danpost

2013/10/14

#
Where (on what line of code) is the error occurring? What line is being highlighted?
Kartoffelbrot Kartoffelbrot

2013/10/14

#
There is no line highlighted. It is showed int the TextBox of System.out.print(); It is likely, that it's from the Serialization class, that I got from a friend: <<Code removed>> Thanks for your help.
danpost danpost

2013/10/14

#
Does it show a system trace? if so, copy/paste here.
Kartoffelbrot Kartoffelbrot

2013/10/15

#
No it doesn't. If you say me, how I can implement that, I will do. And then I can post it here.
danpost danpost

2013/10/15

#
I do not know if I can help. I would think that you would either get a system trace or a line would be highlighted; but, if neither of these are occurring, then I do not know where to look. Did you show everything the terminal window had on it when it came up?
mjrb4 mjrb4

2013/10/15

#
I'm not sure why you want to serialize this particular class, but the rule you're falling foul to here is that the first non-serializable superclass of the class that you're trying to serialize must have a default (no-arg) constructor. In this case, that superclasss is `World`, which does not have a default constructor. You can work around this issue by providing an interim non-serializable superclass that *does* have a default constructor (and in turn passes its chosen parameters up to the previous superclass) but without more details of what you're trying to do it's difficult to say if serialization is the right approach here.
Kartoffelbrot Kartoffelbrot

2013/10/16

#
@ danpost: I will watch again and post a comment on that. @ mjrb4: I guessed something like that. I want to save the world because I want to save the projects that the user creates here: http://www.greenfoot.org/scenarios/9271 I thought it would be the easiest way to save the world with all its actors and to reload it.
Kartoffelbrot Kartoffelbrot

2013/10/16

#
Should I only save the actors, who are in the world an readd them into a new world after loading?
mjrb4 mjrb4

2013/10/16

#
Should I only save the actors, who are in the world an readd them into a new world after loading?
Yeah... you don't really want to serialize for this, it's not really what serialization is for. Just save the attributes of the various actors that you need, and then write some of your own code to reload them.
Kartoffelbrot Kartoffelbrot

2013/10/16

#
What else should I use for saving then? Writing in a txt?
mjrb4 mjrb4

2013/10/16

#
You could; personally I'd probably store the required data in an XML file - it's really matter of preference.
Kartoffelbrot Kartoffelbrot

2013/10/17

#
Hm, I can't "speak" XML.
There are more replies on the next page.
1
2