Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  When i load my file everything inside the class is null?  (Read 1103 times)
0 Members and 1 Guest are viewing this topic.
Offline sothatsit

Senior Newbie


Projects: 1



« Posted 2012-10-10 00:43:17 »

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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
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);
Offline davedes
« Reply #1 - Posted 2012-10-10 01:35:12 »

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

Offline sothatsit

Senior Newbie


Projects: 1



« Reply #2 - Posted 2012-10-10 02:12:12 »

so i shouldnt use it? but im pretty sure when i dont use it i get a not serializable error
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-10-10 03:38:37 »

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?
Offline sothatsit

Senior Newbie


Projects: 1



« Reply #4 - Posted 2012-10-10 03:46:55 »

i imported point from java.awt.Point
Offline sothatsit

Senior Newbie


Projects: 1



« Reply #5 - Posted 2012-10-10 03:47:34 »

so yes it is serializable

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

JGO Coder


Medals: 23



« Reply #6 - Posted 2012-10-10 04:58:51 »

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:

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  
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.
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #7 - Posted 2012-10-10 17:20:38 »

Are you certain the arraylist was populated when you saved it? I'm only asking because I've done boneheaded things like that.

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 Grin

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline actual

JGO Coder


Medals: 23



« Reply #8 - Posted 2012-10-10 20:06:46 »

Interesting but wouldn't my code then not work?
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #9 - Posted 2012-10-10 20:23:21 »

Interesting but wouldn't my code then not work?
1  
2  
3  
      // 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);

Good question...
It ... shouldn't?  Huh ... confused Grin

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...

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!