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?


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 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 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | 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); } } |