TheNickmaster21
TheNickmaster21

Reputation: 275

Object Serialization not working

It does everything it needs to do except save and/or load the data. I'm assuming the issue is with the loading even though the try/catch gets no exceptions. The files are created by the plugin but it won't load the data that was supposedly saved when it was closed...

The complete class can be found here: http://pastebin.com/pK3NCPfM

The loading method:

    public void getClans(){
        try { 
            // Load the Clans
            FileInputStream f_in = new FileInputStream("Clans.data");
            ObjectInputStream obj_in = new ObjectInputStream (f_in);
            @SuppressWarnings("unchecked")
            HashMap<String, Clan> Clans = (HashMap<String, Clan>) obj_in.readObject();
            obj_in.close();
            getLogger().info("Clans successfully loaded");
            } catch (Exception e) {
            getLogger().info("Error loading clan files...");
            getLogger().info(e.getMessage());
            } 
    }

    public void getPlayers(){
        try { 
            // Load the players that are in a clan
            FileInputStream f_in = new FileInputStream("clanPlayers.data");
            ObjectInputStream obj_in = new ObjectInputStream (f_in);
            @SuppressWarnings("unchecked")
            HashMap<String, String> clanPlayers = (HashMap<String, String>) obj_in.readObject();
            obj_in.close();
            getLogger().info("clanPlayers successfully loaded");
            } catch (Exception e) {
            getLogger().info("Error loading clanPlayer files...");
            getLogger().info(e.getMessage());
            } 
    }

UPDATE: I'm not sure why this will not work. If anyone could help me fix the line in question I should be good to go!

    public void getPlayers(){
        try { 
            // Load the players that are in a clan
            FileInputStream f_in = new FileInputStream("clanPlayers.data");
            ObjectInputStream obj_in = new ObjectInputStream (f_in);
            @SuppressWarnings("unchecked")
            clanPlayers = (HashMap<String, String>) obj_in.readObject(); // Error on this line. 
            //I want the HashTable ClanPlayers to be a copy of the object that was saved
            obj_in.close();
            getLogger().info("clanPlayers successfully loaded");
            } catch (Exception e) {
            getLogger().info("Error loading clanPlayer files...");
            getLogger().info(e.getMessage());
            } 
    }

Upvotes: 0

Views: 266

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

These methods don't return anything, and don't initialize any field: only local variables. So they're basically complex noops.

Also, you shouldn't cath Exception. Catch IOException, only if you can really handle the exception at this place. Other wise, let the exception propagate, or transform it into a runeim exception and throw the runtime exception.

Upvotes: 3

Related Questions