When i load my file everything inside the class is null?

so i have a class SaveLevel and i save it fine but when i try to load it again everything inside the class is null?

SaveLevel class


public class SaveLevel  implements Serializable{
	
	static final long serialVersionUID = 99999999999999L;
	
	transient public ArrayList<Point> path = new ArrayList<Point>();
	transient public String imagePath;
	
	public SaveLevel(String imagePath){
		this.imagePath=imagePath;
	}
	
}

Loading Script


Level level = null;
		SaveLevel sLevel = null;
		
		ObjectInputStream ois;
		try {
			ois = new ObjectInputStream(getClass().getResourceAsStream(url));
			sLevel = (SaveLevel) ois.readObject();
			ois.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		System.out.println(sLevel.imagePath);
		System.out.println(sLevel.path);

[quote]transient is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes.
[/quote]
http://en.wikibooks.org/wiki/Java_Programming/Keywords/transient

so i shouldnt use it? but im pretty sure when i dont use it i get a not serializable error

So you threw in transient just to shut up the error without understanding what it’s for or why you got the error in the first place? Yeah … don’t program like that. Ever.

Is Point serializable?

i imported point from java.awt.Point

so yes it is serializable

btw it works without transient just that the array list is empty

Are you certain the arraylist was populated when you saved it? I’m only asking because I’ve done boneheaded things like that. I was able to do the following and it worked:


package serialtest;

import java.awt.Point;
import java.io.Serializable;
import java.util.ArrayList;

public class SaveLevel implements Serializable {

	static final long serialVersionUID = 99999999999999L;
	   
	    public ArrayList<Point> path = new ArrayList<Point>();
	    public String imagePath;
	   
	   public SaveLevel(String imagePath){
	      this.imagePath=imagePath;
	   }
}



package serialtest;

import java.awt.Point;
import java.io.*;
import java.util.ArrayList;

public class SerialTest {


	public static void main(String[] args) {
		
		
	  // Create some Dummy information
      SaveLevel sLevel = new SaveLevel("Hi this is a test");
      
      
      ArrayList<Point> points = new ArrayList<Point>();
      points.add(new Point(1,1));
      points.add(new Point(2,2));
      points.add(new Point(3,3));
      
      sLevel.path = points;   
      
      String fileName ="c:\\level.dat";
   
      
      // Write the object to disk
      try {
         FileOutputStream fileOut = new FileOutputStream(fileName);
         ObjectOutputStream out =  new ObjectOutputStream(fileOut);
         out.writeObject(sLevel);
         out.close();
          fileOut.close();
      }catch(IOException i) {
          i.printStackTrace();
      }    
      
      // Make an empty saved level
      SaveLevel newLevel = new SaveLevel("");
      
      
      // Try to read the level back from the file
      ObjectInputStream ois;
      try {
    	  FileInputStream fileIn =  new FileInputStream(fileName);
    	  ois = new ObjectInputStream(fileIn);
      
         SaveLevel loadedLevel = (SaveLevel) ois.readObject();
         newLevel = loadedLevel;
         ois.close();
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      }
      
      
      // This should print out "Hi this is a test" and 3 points if it worked.
      System.out.println(newLevel.imagePath);
      System.out.println(newLevel.path);
	}

}

This worked (the restored arraylist has 3 elements). The main difference is that I didn’t use the getResource method to create the objectinputstream.

The array used to store objects inside the arrayList is transient.
Reading the source code of the SE once as a java developer is just like the mecca for muslims ;D

Interesting but wouldn’t my code then not work?

Good question…
It … shouldn’t? ??? … confused ;D

EDIT: Okey… very quick edit (my Google-fu skillz are awezome…) Stackoverflow question bout that

I’m looking at the Serializable spec now… propably the ArrayList is treated special…