0

My question is about unserializing a file that is created using one class "Character" and a GUI form "NewChar".

I'm relatively new to Java and trying to create a DnD style character generator. I have three classes that I'm working with currently - Character (the java class that houses the actual stats like name, gender, class, str, dex, etc.) that includes a constructor; a GUI form (NewChar, created using NetBeans and Swing) that allows the user to select the stats as defined in the Character class and customize them as desired; and the CharMain GUI page that should (eventually) show the user their character and allow them to level up, choose a weapon, etc.

As of right now, I can successfully serialize the data by instantiating the class Character within the NewChar GUI frame (as far as I can tell). I am as of yet unable to load the serialized file from my CharMain GUI page. Primarily, the issue seems to be the ClassNotFoundException when I try to readObject(); on any of the strings I've tried to serialize. Secondarily, I continue to receive the EOF exception although I suspect that's because the amount of items serialized and then unserialized doesn't match since the readObject(); isn't working. I've googled and searched StackOverFlow and found a few responses to similar questions (ex: readobject method throws ClassNotFoundException or Java Mysterious EOF exception with readObject) but can't make heads or tails of the responses given with respect to my situation.

I have tried to copy and paste my Character class into the two GUI forms but that doesn't seem to have helped. I've also tried serializing only the instance of the class Character (instance called createdChar) but that didn't seem to help either.

I know that the objects I'm trying to deserialize needs to match the objects that have been serialized (i.e. package, class name, etc.) but as far as I can tell they do. I'm probably being super-blind and/or stupid-oblivious but my in-house fellow coder can't seem to figure it out either. At this point I'm thinking about writing the info to a text file and reading that back instead of utilizing the ObjectOutputStream and ObjectInputStream but would still really like to know why this doesn't work. Please help!

Here is the code to serialize (and then de-serialize) the data:

private void viewCharActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    String fileName = String.valueOf(createdChar.charName);
    try {
        FileOutputStream charSave = new FileOutputStream("/C:\\javaSaves\\newChar.sav");
        ObjectOutputStream out = new ObjectOutputStream(charSave);

        out.writeObject(createdChar.str);
        out.writeObject(createdChar.dex);
        out.writeObject(createdChar.con);
        out.writeObject(createdChar.intel);
        out.writeObject(createdChar.wis);
        out.writeObject(createdChar.charisma);
        out.writeObject(createdChar.strMod);
        out.writeObject(createdChar.dexMod);
        out.writeObject(createdChar.conMod);
        out.writeObject(createdChar.intelMod);
        out.writeObject(createdChar.wisMod);
        out.writeObject(createdChar.charisMod);
        out.writeObject(createdChar.baseAttack);
        out.writeObject(createdChar.reflexSave);
        out.writeObject(createdChar.willSave);
        out.writeObject(createdChar.fortSave);
        out.writeObject(createdChar.hp);
        out.writeObject(createdChar.lvl);
        out.writeObject(createdChar.speed);
        out.writeObject(createdChar.armorClass);
        out.writeObject(createdChar.size);
        out.writeObject(createdChar.charName);
        out.writeObject(createdChar.charRace);
        out.writeObject(createdChar.charClass);
        out.writeObject(createdChar.gender);

        out.close();
        charSave.close();
        System.out.printf("Serialized data is saved on C:\\javaSaves\\newChar.sav");
   }
    catch (IOException i) {
        i.printStackTrace();
    }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new charMain().setVisible(true);
            }
        });

        closeFrame();
}  

public void loadCharacterFromFile(){
    try {
        FileInputStream saveFile = new FileInputStream("/C:\\javaSaves\\newChar.sav");
        ObjectInputStream save = new ObjectInputStream(saveFile);
            createdChar.str = save.readInt();
            createdChar.dex = save.readInt();
            createdChar.con = save.readInt();
            createdChar.intel = save.readInt();
            createdChar.wis = save.readInt();
            createdChar.charisma = save.readInt();
            createdChar.strMod = save.readInt();
            createdChar.dexMod = save.readInt();
            createdChar.conMod = save.readInt();
            createdChar.intelMod = save.readInt();
            createdChar.wisMod = save.readInt();
            createdChar.charisMod = save.readInt();
            createdChar.baseAttack = save.readInt();
            createdChar.reflexSave = save.readInt();
            createdChar.willSave = save.readInt();
            createdChar.fortSave = save.readInt();
            createdChar.hp = save.readInt();
            createdChar.lvl = save.readInt();
            createdChar.speed = save.readInt();
            createdChar.armorClass = save.readInt();
            createdChar.size = (String) save.readObject();
            createdChar.charName = (String) save.readObject();    
            createdChar.charRace = (String) save.readObject();
            createdChar.charClass = (String) save.readObject();
            createdChar.gender = (String) save.readObject();

        save.close();
        saveFile.close();
    }
    catch (IOException i) {
        i.printStackTrace();
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
  • It is exactly as per the duplicate. The class named in the exception wasn't available on the CLASSPATHS when deserializing. There's nothing different about your situation. – user207421 Aug 10 '17 at 03:56
  • Thank you for answering!! I am probably still wrong/being really thick right now, but does it matter at all if the NewChar class/form is actually modifying the variables in the Character Class? I tried to separate out the logic from the GUI as much as possible, but I instantiate an instance of the Character class inside the NewChar class/form and that's what I actually want to serialize and read. I am not sure how to make a "shared.Character" class that will actually have the correct info. Or is that perhaps more a programmatic issue in terms of how my information is organized? – aliceInJavaLand Aug 12 '17 at 19:41

0 Answers0