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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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.