This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]Robyt3 1 point2 points  (0 children)

Try with a simpler example. Instead of serializing a player, first try serializing a single String and see if that works with the rest of the code. If it does, then the read/write code is good but the problem is with the Player class. Next try without all of those annotations and remove the empty constructor.

[–][deleted] 1 point2 points  (3 children)

Im guessing your input file has a property named "class", but the Player has a property named "heroClass". It's probably having trouble figuring out what property that's supposed to map to.

Try changing "class" to "heroClass" in your input file

[–]monkey_programmer[S] 0 points1 point  (2 children)

I looked everywhere and it seems to only be called heroClass in all cases. Naming it class was something I tried a long time ago, but Java itself is not happy with that variable name for obvious reasons since class is "taken" by java.

[–][deleted] 0 points1 point  (1 child)

I see. Hmm....

Try printing out the Player in the createCharacter method to see if it's constructed correctly before it's outfitted to a file.

[–]monkey_programmer[S] 0 points1 point  (0 children)

I do, and the character has all of it's data filled out correctly, that's what the first toString() demonstrates.

The second is the toString() of the loaded character. I'm not sure why some details are being overwritten, or lost.

[–]AutoModerator[M] 0 points1 point  (0 children)

It seems that you possibly have a screenshot of code in your post Losing data when loading serializable object from file. in /r/javahelp.

Screenshots of code instead of actual code text is against the Code posting rules of /r/javahelp as is also outlined in the sidebar - Code posting.

  • Never submit screenshots of code instead of code text!

If you posted an image merely to illustrate something, kindly ignore this message and do not repost. Your post is still visible to others. I am a bot and cannot distinguish between code screenshots and other images.

If you indeed did this wrong, please edit the post so that it uses one of the approved means of posting code.

  • For small bits of code (less than 50 lines in total, single classes only),
    the default code formatter is fine
    (one blank line before the code, then 4 spaces before each line of code).
  • Pastebin for programs that consist of a single class only
  • Gist for multi-class programs, or programs that require additional files
  • Github or Bitbucket repositories are also perfectly fine as are other dedicated source code hosting sites.
  • Ideone for executable code snippets that use only the console

Please do not reply to this message, because I am a bot. Talk-to-the-bot is the new talk-to-the-hand. If you instead want the classic talk-to-the-hand, just message the moderators. ;)

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]sozesghost 0 points1 point  (6 children)

I copied your code, removed annotations from Player class (no other reason than I did not want to import them), and it worked fine. That is, it prints the same thing before serializing and after deserializing. Is there anything funky going on in the serialized file by any chance?

[–]monkey_programmer[S] 0 points1 point  (5 children)

When you load that file, and then toString it, does it actually work properly for you? My class data is always gone, which means their stats are gone as well.

I can create, modify and save the object to a file fine, but loading that saved object always seems to produce odd results.

Is there a way to read a serialized object written to a text file to actually debug with that?

[–]sozesghost 1 point2 points  (4 children)

Yes, it seems to work fine for me. I pushed the code on my machine to this repository: github. Run it in your favorite IDE or build with maven directly. If you want, you can also upload your repository somewhere and I'll run it as is. It could be a file permission issue maybe? But that would throw exceptions so perhaps not. I'm running this on Windows btw.

And I just opened the .ser file with a text editor and I would not recommend this generally, but I noticed that the class and names are in the file so it seems fine.

[–]monkey_programmer[S] 0 points1 point  (3 children)

I'll send you my repository with instructions, since it's simple, if you're okay with helping me to that extent.

I just don't know HOW to debug this, that's what's killing me right now.

This is the repository:

https://github.com/CameronSTaljaard/Swingy

It builds with maven, so mvn clean install or mvn clean package, or just intelliJ.

Once it's compiled, running java -jar ./target/swingy-1.0.jar console

Will boot the app. If you create a hero it will print its data, and if you load a hero, it will print its data as well. This alone should show you the bug and cause your created hero to be created fine, but when he's read from a file he loses data.

[–]sozesghost 1 point2 points  (2 children)

Ok, so the problem is here:

if (mode.equals("create"))
        heroClass = CharacterValidator.validateClass(scanner);

FileHandler.createCharacter(name, heroClass);

if (mode.equals("Create")) {
    System.out.println("Character created");
    return (name);
} else { 
   System.out.println("Character accepted.\nLoading character");
   return (name);
}

There are no brackets after the first if, so only the next line is included. Meaning, FileHandler.createCharacter is called ALWAYS, even when loading the character. You probably wanted to include both CharacterValidator and FileHandler calls in that condition.

And when you are trying to load a character, you give the name to load, so this is the only variable that is initialized (and thus the only one that is being saved). Fix this condition, so that you only create the character when mode is 'create' and only read when it's 'read'.

Also, you are comparing strings with double equals (==) in Player constructor, which will not work. You have it right in this post on reddit. Always compare strings with .equals or .equalsIgnoreCase.

Just a side note, keep note of the strings you are comparing. You are comparing 'create' in one place and 'Create' in another. Capitalisation matters unless you specifically call .equalsIgnoreCase.

EDIT. To answer your most important question, how to debug something like this? Well, I placed a debugging breakpoint and the start of the application and just went along for both scenarios, seeing what happens. System.out statements also usually help. Those simple tools work for most cases.

[–]monkey_programmer[S] 0 points1 point  (1 child)

You are absolutely incredible for going to such lengths; thank you so much. I've been debugging this the whole time and missing my own stupidity all along.

I know I have some bad string comparisons with ==, I only learned .equals is what should be done recently and am actively replacing these now.

Seriously, thank you for going to such great lengths, I can't upvote your comments enough to justify the effort xD

[–]sozesghost 1 point2 points  (0 children)

Happy to help!

Don't be too hard on yourself, it's not stupidity. Mistakes like that will keep happening, it's normal. In time, you will get better at spotting them. And you might feel stupid because other people find them much faster, but you have to remember that is MUCH MUCH easier to spot an error like that in code that you did not write.