java - HashMap 数组未反序列化

标签 java serialization hashmap

我有一个 JavaFX 程序,它接受用户的输入并将其保存到用户选择的序列化文件中。用户保存的任何文件都可以反序列化、编辑或查看。这一切都是通过将数据存储在 HashMap 中并序列化 HashMap 数组来完成的。

发生的情况是数据正在正确收集,但如果退出程序并重新启动,则无法恢复。我检查了序列化文件,它似乎正在保存,但未加载。

这是一个非常大的 JavaFX 程序,我只包含了使用/处理 HashMap 的三个类。我相信这更多的是一个数据结构问题,与我设置 HashMap 的方式有关,而不是 JavaFX 问题。

这是创建 HashMap 的类:

`package CanavanCalculator;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

import javafx.scene.control.ComboBox;

public class Data extends Start {
    // public static final long serialVersionUID = -2843350657533110180L;
    // for hashmaps, use index integer for plate Appearence number

    public HashMap<Integer, Integer> Balls = new HashMap<Integer, Integer>(); 
    public HashMap<Integer, Integer> Strikes = new HashMap<Integer, Integer>();
    public HashMap<Integer, Integer> Pitches = new HashMap<Integer, Integer>();
    public HashMap<Integer, String> PitchType = new HashMap<Integer, String>();
    public HashMap<Integer, String> PitchLocation = new HashMap<Integer, String>();
    public HashMap<Integer, String> HitQuality = new HashMap<Integer, String>();
    public HashMap<Integer, String> HitType = new HashMap<Integer, String>();
    public HashMap<Integer, String> HitDirection = new HashMap<Integer, String>();
    public HashMap<Integer, String> PitcherNames = new HashMap<Integer, String>();
    public HashMap<Integer, Boolean> PitcherOrientation = new HashMap<Integer, Boolean>();
    // true = Right handed; false = left handed
    public static HashMap<Integer, String> Opponent = new HashMap<Integer, String>();
    public static HashMap<Integer, String> TeamList = new HashMap<Integer, String>();
    public  HashMap[] arr = { Balls, Strikes, Pitches, PitchType, PitchLocation, HitQuality, HitType, HitDirection,
            PitcherNames, PitcherOrientation };
    //public String[] str= {"Balls","Strikes","Pitches","PitchType","PitchLocation","HitQuality","HitType","HitDirection","PitcherNames","PitcherOrientation"}; 

    static HashMap<Integer, String> oldList = new HashMap<Integer, String>();

    public static String temp = "null";

    public  void debug() {
        for (HashMap hshmp : arr) {
            System.out.println(hshmp);
        }
    }


    public String Percent(int dividend, int divisor) {
        double d = (double) dividend / divisor;
        if (dividend == 0 && divisor == 0) {
            return "0.0%";
        } else {
            d *= 100;
            String returned = d + "%";
            return returned;
        }
    }

    public static String mergedata(int freq, String percent) {
        return freq + ": " + percent;
    }


    public void save(String Name) {
        if (Name != null) {
            try (FileOutputStream fos = new FileOutputStream(Name+".ser");
                    ObjectOutputStream OOs = new ObjectOutputStream(fos)) {
                System.out.println(arr);
                OOs.writeObject(arr);
                debug();
            } catch (FileNotFoundException fnfe) {
                fnfe.printStackTrace();
                System.out.println(fnfe.getMessage());
            } catch (IOException Io) {
                Io.printStackTrace();
                System.out.println(Io.getMessage());
            }
        }
    }


    public void open(String Name) {
        try (FileInputStream fis = new FileInputStream(Name+".ser");
                ObjectInputStream ois = new ObjectInputStream(fis)) {
             arr = (HashMap[]) ois.readObject();
            temp = Name;
            debug();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.out.print(ioe.getMessage());
        } catch (ClassNotFoundException cnfe) {
            System.out.println(cnfe.getMessage());
            cnfe.printStackTrace();
        }
    }


    public static void saveteamname() {
        try (FileOutputStream fos = new FileOutputStream(toFile("Team0ra"));
                ObjectOutputStream OOs = new ObjectOutputStream(fos)) {
            OOs.writeObject(TeamList);
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
            System.out.println(fnfe.getMessage());
        } catch (IOException Io) {
            Io.printStackTrace();
            System.out.println(Io.getMessage());
        }
    }

    public void loadteamname() {
        try {
            FileInputStream fis = new FileInputStream(toFile("Team0ra"));
            ObjectInputStream ois = new ObjectInputStream(fis);
            clear();
            TeamList = (HashMap<Integer, String>) ois.readObject();
            System.out.println(TeamList);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.out.print(ioe.getMessage());
        } catch (ClassNotFoundException cnfe) {
            TeamList.put(1, "ME");
            saveteamname();
            System.out.println(cnfe.getMessage());
            cnfe.printStackTrace();
        }
    }

    public void clear() {
        for (HashMap hash : arr) {
            hash.clear();
        }
    }

    public static String toFile(String name/* hitters from the hashmap */) {
        return name + ".ser";
    }

    public static  void addTeamListtobox(ComboBox<String> cb) {
        System.out.println(TeamList);
        for (int i = 1; i <= TeamList.size(); i++) {
            if (TeamList.get(i) != null) {
                cb.getItems().add(TeamList.get(i));
            }
        }

    }

    public static int countif(HashMap Map, String check, HashMap Map1, String check1) {
        int returne = 0;
        for (int i = -1; i <= Map.size(); i++) {
            if (Map.get(i) != null && Map1.get(i) != null) {
                if (((String) Map.get(i)).equalsIgnoreCase(check) && ((String) Map1.get(i)).equalsIgnoreCase(check1)) {
                    returne++;
                }
            }
        }
        return returne;

    }

    public static  int countif(HashMap Map, int check, HashMap Map1, String check1) {
        int returne = 0;
        for (int i = -1; i <= Map.size(); i++) {
            if (Map.get(i) != null && Map1.get(i) != null) {
                if (((int) Map.get(i)) == check && ((String) Map1.get(i)).equalsIgnoreCase(check1)) {
                    returne++;
                }
            }
        }
        return returne;

    }

    public static int countif(HashMap Map, int check, HashMap Map1, int check1, HashMap map2, String check2) {
        int returne = 0;
        for (int i = -1; i <= Map.size(); i++) {
            if (Map.get(i) != null && Map1.get(i) != null) {
                if (((int) Map.get(i)) == check && ((int) Map1.get(i)) == check1
                        && ((String) map2.get(i)).equals(check2)) {
                    returne++;
                }
            }
        }
        return returne;

    }

    public static  int countif(HashMap Map, int check, HashMap Map1, int check1) {
        int returne = 0;
        for (int i = -1; i <= Map.size(); i++) {
            if (Map.get(i) != null && Map1.get(i) != null) {
                if ((int) Map.get(i) == check && (int) Map1.get(i) == check1) {
                    returne++;
                }
            }
        }
        return returne;

    }

    public static int countif(HashMap Map, int check) {
        int returne = 0;
        for (int i = -1; i <= Map.size(); i++) {
            if (Map.get(i) != null) {
                if ((int) Map.get(i) == check) {
                    returne++;
                }
            }
        }
        return returne;

    }

    public static int countif(HashMap Map, String check) {
        int returne = 0;
        for (int i = -1; i <= Map.size(); i++) {
            if (Map.get(i) != null) {
                if (((String) Map.get(i)).equalsIgnoreCase(check)) {
                    returne++;
                }
            }
        }
        return returne;

    }
}
`

这是第一个 Controller :

 package ------;
    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Button;
    import javafx.scene.control.ComboBox;
    import javafx.scene.control.TextField; 

public class Controller extends Data implements Initializable {

    // variables for calculations
    protected boolean contact;
    protected int ex1;
    // variables from FXes
    @FXML
    ComboBox<String> InPitchType;
    @FXML
    ComboBox<String> InPitchLocation;
    @FXML
    ComboBox<String> InHitQuality;
    @FXML
    ComboBox<String> InHitType;
    @FXML
    ComboBox<String> InHitDirection;
    @FXML
    ComboBox<String> InPitcherHand;
    @FXML
    ComboBox<String> InHitter;
    @FXML
    TextField PitcherName;
    @FXML
    TextField InPitcher;
    @FXML
    TextField InBalls;
    @FXML
    TextField InStrikes;
    @FXML
    Button AddData;
    @FXML
    TextField InOpponent;
    protected void closesafe() {// not finished
        // create the new files
        save(temp);
        debug();
        int newent = TeamList.size() - oldList.size();
        for (int i = 0; i <= newent; i++) {
            if (null != TeamList.get(oldList.size() + i)) {
                save(TeamList.get(oldList.size() + i));
            }

        }
        saveteamname();
        System.out.println(TeamList);
        stage.close();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        InPitchType.getItems().addAll("Change Up", "FastBall", "CurveBall", "Slider");
        InPitchLocation.getItems().addAll("Ball-Inside-High", "Ball-Inside-low", "Ball-Outside-High",
                "Ball-Outside-Low", "Ball-Middle Inside-High", "Ball-Middle Outside-High", "Ball-Inside-Low",
                "Ball-Outside-Low", "Strike-Inside-High", "Strike-Inside-Low", "Strike-Outside-Low",
                "Strike-Outside-High", "Strike-Center", "Strike-Center-High", "Strike-Center-Low");
        InHitQuality.getItems().addAll("Hard Hit", "Routine Hit", "Weak Hit", "Standing Strikeout", "Strikeout", "Walk",
                "Hit By Pitch");
        InHitType.getItems().addAll("Grounder", "Line-Drive", "Fly");
        InHitDirection.getItems().addAll("Pitcher", "Catcher", "First Base", "Second Base", "Shortstop", "Third Base",
                "Right Field", "Center Field", "Left Field");
        InPitcherHand.getItems().addAll("Left Handed", "Right Handed");
        loadteamname();
        oldList = TeamList;
        addTeamListtobox(InHitter);
        stage.setOnCloseRequest(e -> closesafe());
        System.out.println(TeamList);
    }

    // event handlers
    public void AddValue(ActionEvent actionevent) {
        ex1 = Pitches.size() + 1;
        System.out.println(ex1);
        try {
            InBalls.getText();
            InStrikes.getText();
        } catch (NumberFormatException nfe) {
            System.out.println(nfe.getMessage());
            nfe.printStackTrace();
        }
        Balls.put(ex1, Integer.parseInt(InBalls.getText()));
        Strikes.put(ex1, Integer.parseInt(InStrikes.getText()));
        Pitches.put(ex1, Integer.parseInt(InStrikes.getText()) + Integer.parseInt(InBalls.getText()));
        PitchType.put(ex1, InPitchType.getValue());
        PitchLocation.put(ex1, InPitchLocation.getValue());
        HitQuality.put(ex1, InHitQuality.getValue());
        if (contact) {
            // if the ball was actually hit, then add the other stuff
            HitType.put(ex1, InHitType.getValue());
            HitDirection.put(ex1, InHitDirection.getValue());
        } else {// if not, dont
            HitType.put(ex1, null);
            HitDirection.put(ex1, null);
        }
        debug();
        PitcherNames.put(ex1, PitcherName.getText());
        Opponent.put(ex1, InOpponent.getText());
        if (InPitcherHand != null && InPitcherHand.getValue() == "Left Handed") {
            PitcherOrientation.put(ex1, false);
        } else if (InPitcherHand != null && InPitcherHand.getValue() == "Right Handed") {
            PitcherOrientation.put(ex1, true);
        }
        System.out.println("Ran and added");
//      InBalls.clear();
//      InStrikes.clear();
//      PitcherNames.clear();
//      Opponent.clear();
    }

    public void GoToAbout(ActionEvent actionevent) {
        gotoabout();
    }

    public void GoToData(ActionEvent actionevent) {
        gotodata();
    }

    public void CheckValueType(ActionEvent actionevent) {
        if (InHitQuality.getValue().equals("Hit By Pitch") || (InHitType.getValue().equals("Walk")
                || (InHitType.getValue().equals("Strikeout") || (InHitType.getValue().equals("Standing Strikeout"))))) {
            contact = false;
        } else {
            contact = true;
        }
    }

    public void CheckHitter(ActionEvent actionevent) {
        save(temp);
        System.out.println("before:");
        debug();
        if (InHitter.getValue() != null) {
            open(InHitter.getValue());
            System.out.println("after:");
            debug();
        }
    }

}

然后这是最后一个 Controller :

package CanavanCalculator;

import java.net.URL;
import java.util.HashMap;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.text.Text;

public class contOutput extends Data implements Initializable {
    protected void closesafe() {// not finished
        // create the new files
        save(temp);
        debug();
        int newent = TeamList.size() - oldList.size();
        for (int i = 0; i <= newent; i++) {
            if (null != TeamList.get(oldList.size() + i)) {
                save(TeamList.get(oldList.size() + i));
            }

        }
        saveteamname();
        System.out.println(TeamList);
        stage.close();
    }
    @FXML
    Button AddData;
    @FXML
    ComboBox<String> ListOTeam;
    // formula for aiming Text Fields:
    // page,column name,row number
    /* Number of Pitches page */@FXML
    Text NPF1, NPF2, NPF3, NPF4, NPF5, NPF6, NPHH1, NPHH2, NPHH3, NPHH4, NPHH5, NPHH6, NPRH1, NPRH2, NPRH3, NPRH4,
            NPRH5, NPRH6, NPWH1, NPWH2, NPWH3, NPWH4, NPWH5, NPWH6, NPSS1, NPSS2, NPSS3, NPSS4, NPSS5, NPSS6, NPS1,
            NPS2, NPS3, NPS4, NPS5, NPS6, NPW1, NPW2, NPW3, NPW4, NPW5, NPW6, NPHBP1, NPHBP2, NPHBP3, NPHBP4, NPHBP5,
            NPHBP6, NPLD1, NPLD2, NPLD3, NPLD4, NPLD5, NPLD6, NPG1, NPG2, NPG3, NPG4, NPG5, NPG6, NPFL1, NPFL2, NPFL3,
            NPFL4, NPFL5, NPFL6;
    Text[][] NP = { { NPF1, NPF2, NPF3, NPF4, NPF5, NPF6 }, { NPHH1, NPHH2, NPHH3, NPHH4, NPHH5, NPHH6 },
            { NPRH1, NPRH2, NPRH3, NPRH4, NPRH5, NPRH6 }, { NPWH1, NPWH2, NPWH3, NPWH4, NPWH5, NPWH6 },
            { NPSS1, NPSS2, NPSS3, NPSS4, NPSS5, NPSS6 }, { NPS1, NPS2, NPS3, NPS4, NPS5, NPS6 },
            { NPW1, NPW2, NPW3, NPW4, NPW5, NPW6 }, { NPHBP1, NPHBP2, NPHBP3, NPHBP4, NPHBP5, NPHBP6 },
            { NPLD1, NPLD2, NPLD3, NPLD4, NPLD5, NPLD6 }, { NPG1, NPG2, NPG3, NPG4, NPG5, NPG6 },
            { NPFL1, NPFL2, NPFL3, NPFL4, NPFL5, NPFL6 } };

    /* Count */@FXML
    Text CTF1, CTF2, CTF3, CTF4, CTF5, CTF6, CTF7, CTF8, CTF9, CTF10, CTF11, CTF12, CTHH1, CTHH2, CTHH3, CTHH4, CTHH5,
            CTHH6, CTHH7, CTHH8, CTHH9, CTHH10, CTHH11, CTHH12, CTRH1, CTRH2, CTRH3, CTRH4, CTRH5, CTRH6, CTRH7, CTRH8,
            CTRH9, CTRH10, CTRH11, CTRH12, CTWH1, CTWH2, CTWH3, CTWH4, CTWH5, CTWH6, CTWH7, CTWH8, CTWH9, CTWH10,
            CTWH11, CTWH12, CTSS1, CTSS2, CTSS3, CTSS4, CTSS5, CTSS6, CTSS7, CTSS8, CTSS9, CTSS10, CTSS11, CTSS12, CTS1,
            CTS2, CTS3, CTS4, CTS5, CTS6, CTS7, CTS8, CTS9, CTS10, CTS11, CTS12, CTW1, CTW2, CTW3, CTW4, CTW5, CTW6,
            CTW7, CTW8, CTW9, CTW10, CTW11, CTW12, CTHBP1, CTHBP2, CTHBP3, CTHBP4, CTHBP5, CTHBP6, CTHBP7, CTHBP8,
            CTHBP9, CTHBP10, CTHBP11, CTHBP12, CTLD1, CTLD2, CTLD3, CTLD4, CTLD5, CTLD6, CTLD7, CTLD8, CTLD9, CTLD10,
            CTLD11, CTLD12, CTGD1, CTGD2, CTGD3, CTGD4, CTGD5, CTGD6, CTGD7, CTGD8, CTGD9, CTGD10, CTGD11, CTGD12,
            CTFL1, CTFL2, CTFL3, CTFL4, CTFL5, CTFL6, CTFL7, CTFL8, CTFL9, CTFL10, CTFL11, CTFL12;
    Text[][] CT = { { CTF1, CTF2, CTF3, CTF4, CTF5, CTF6, CTF7, CTF8, CTF9, CTF10, CTF11, CTF12 },
            { CTHH1, CTHH2, CTHH3, CTHH4, CTHH5, CTHH6, CTHH7, CTHH8, CTHH9, CTHH10, CTHH11, CTHH12 },
            { CTRH1, CTRH2, CTRH3, CTRH4, CTRH5, CTRH6, CTRH7, CTRH8, CTRH9, CTRH10, CTRH11, CTRH12 },
            { CTWH1, CTWH2, CTWH3, CTWH4, CTWH5, CTWH6, CTWH7, CTWH8, CTWH9, CTWH10, CTWH11, CTWH12 },
            { CTSS1, CTSS2, CTSS3, CTSS4, CTSS5, CTSS6, CTSS7, CTSS8, CTSS9, CTSS10, CTSS11, CTSS12 },
            { CTS1, CTS2, CTS3, CTS4, CTS5, CTS6, CTS7, CTS8, CTS9, CTS10, CTS11, CTS12 },
            { CTW1, CTW2, CTW3, CTW4, CTW5, CTW6, CTW7, CTW8, CTW9, CTW10, CTW11, CTW12 },
            { CTHBP1, CTHBP2, CTHBP3, CTHBP4, CTHBP5, CTHBP6, CTHBP7, CTHBP8, CTHBP9, CTHBP10, CTHBP11, CTHBP12 },
            { CTLD1, CTLD2, CTLD3, CTLD4, CTLD5, CTLD6, CTLD7, CTLD8, CTLD9, CTLD10, CTLD11, CTLD12 },
            { CTGD1, CTGD2, CTGD3, CTGD4, CTGD5, CTGD6, CTGD7, CTGD8, CTGD9, CTGD10, CTGD11, CTGD12 },
            { CTFL1, CTFL2, CTFL3, CTFL4, CTFL5, CTFL6, CTFL7, CTFL8, CTFL9, CTFL10, CTFL11, CTFL12 } };

    /* Pitch Type */
    @FXML
    Text PTF1, PTF2, PTF3, PTF4, PTHH1, PTHH2, PTHH3, PTHH4, PTRH1, PTRH2, PTRH3, PTRH4, PTWH1, PTWH2, PTWH3, PTWH4,
            PTSS1, PTSS2, PTSS3, PTSS4, PTS1, PTS2, PTS3, PTS4, PTW1, PTW2, PTW3, PTW4, PTHBP1, PTHBP2, PTHBP3, PTHBP4,
            PTLD1, PTLD2, PTLD3, PTLD4, PTGD1, PTGD2, PTGD3, PTGD4, PTFL1, PTFL2, PTFL3, PTFL4;
    Text[][] PT = { { PTF1, PTF2, PTF3, PTF4 }, { PTHH1, PTHH2, PTHH3, PTHH4 }, { PTRH1, PTRH2, PTRH3, PTRH4 },
            { PTWH1, PTWH2, PTWH3, PTWH4 }, { PTSS1, PTSS2, PTSS3, PTSS4 }, { PTS1, PTS2, PTS3, PTS4 },
            { PTW1, PTW2, PTW3, PTW4 }, { PTHBP1, PTHBP2, PTHBP3, PTHBP4 }, { PTLD1, PTLD2, PTLD3, PTLD4 },
            { PTGD1, PTGD2, PTGD3, PTGD4 }, { PTFL1, PTFL2, PTFL3, PTFL4 } };
    /* Pitch Location */
    @FXML
    Text PLF1, PLF2, PLF3, PLF4, PLF5, PLF6, PLF7, PLF8, PLF9, PLF10, PLF11, PLF12,PLF13,PLF14,PLF15, PLHH1, PLHH2, PLHH3, PLHH4, PLHH5,
            PLHH6, PLHH7, PLHH8, PLHH9, PLHH10, PLHH11, PLHH12,PLHH13,PLHH14,PLHH15, PLRH1, PLRH2, PLRH3, PLRH4, PLRH5, PLRH6, PLRH7, PLRH8,
            PLRH9, PLRH10, PLRH11, PLRH12,PLRH13,PLRH14,PLRH15, PLWH1, PLWH2, PLWH3, PLWH4, PLWH5, PLWH6, PLWH7, PLWH8, PLWH9, PLWH10,
            PLWH11, PLWH12,PLWH13,PLWH14,PLWH15, PLSS1, PLSS2, PLSS3, PLSS4, PLSS5, PLSS6, PLSS7, PLSS8, PLSS9, PLSS10, PLSS11, PLSS12,PLSS13,PLSS14,PLSS15, PLS1,
            PLS2, PLS3, PLS4, PLS5, PLS6, PLS7, PLS8, PLS9, PLS10, PLS11, PLS12,PLS13,PLS14,PLS15, PLW1, PLW2, PLW3, PLW4, PLW5, PLW6,
            PLW7, PLW8, PLW9, PLW10, PLW11, PLW12,PLW13,PLW14,PLW15, PLHBP1, PLHBP2, PLHBP3, PLHBP4, PLHBP5, PLHBP6, PLHBP7, PLHBP8,
            PLHBP9, PLHBP10, PLHBP11, PLHBP12,PLHBP13,PLHBP14,PLHBP15, PLLD1, PLLD2, PLLD3, PLLD4, PLLD5, PLLD6, PLLD7, PLLD8, PLLD9, PLLD10,
            PLLD11, PLLD12,PLLD13,PLLD14,PLLD15, PLGD1, PLGD2, PLGD3, PLGD4, PLGD5, PLGD6, PLGD7, PLGD8, PLGD9, PLGD10, PLGD11, PLGD12,PLGD13,PLGD14,PLGD15,
            PLFL1, PLFL2, PLFL3, PLFL4, PLFL5, PLFL6, PLFL7, PLFL8, PLFL9, PLFL10, PLFL11, PLFL12,PLFL13,PLFL14,PLFL15;
    Text[][] PL = { { PLF1, PLF2, PLF3, PLF4, PLF5, PLF6, PLF7, PLF8, PLF9, PLF10, PLF11, PLF12,PLF13,PLF14,PLF15 },
            { PLHH1, PLHH2, PLHH3, PLHH4, PLHH5, PLHH6, PLHH7, PLHH8, PLHH9, PLHH10, PLHH11, PLHH12,PLHH13,PLHH14,PLHH15 },
            { PLRH1, PLRH2, PLRH3, PLRH4, PLRH5, PLRH6, PLRH7, PLRH8, PLRH9, PLRH10, PLRH11, PLRH12,PLRH13,PLRH14,PLRH15 },
            { PLWH1, PLWH2, PLWH3, PLWH4, PLWH5, PLWH6, PLWH7, PLWH8, PLWH9, PLWH10, PLWH11, PLWH12,PLWH13,PLWH14,PLWH15 },
            { PLSS1, PLSS2, PLSS3, PLSS4, PLSS5, PLSS6, PLSS7, PLSS8, PLSS9, PLSS10, PLSS11, PLSS12,PLSS13,PLSS14,PLSS15 },
            { PLS1, PLS2, PLS3, PLS4, PLS5, PLS6, PLS7, PLS8, PLS9, PLS10, PLS11, PLS12,PLS13,PLS14,PLS15 },
            { PLW1, PLW2, PLW3, PLW4, PLW5, PLW6, PLW7, PLW8, PLW9, PLW10, PLW11, PLW12,PLW13,PLW14,PLW15 },
            { PLHBP1, PLHBP2, PLHBP3, PLHBP4, PLHBP5, PLHBP6, PLHBP7, PLHBP8, PLHBP9, PLHBP10, PLHBP11, PLHBP12 ,PLHBP13,PLHBP14,PLHBP15},
            { PLLD1, PLLD2, PLLD3, PLLD4, PLLD5, PLLD6, PLLD7, PLLD8, PLLD9, PLLD10, PLLD11, PLLD12,PLLD13,PLLD14,PLLD15 },
            { PLGD1, PLGD2, PLGD3, PLGD4, PLGD5, PLGD6, PLGD7, PLGD8, PLGD9, PLGD10, PLGD11, PLGD12,PLGD13,PLGD14,PLGD15 },
            { PLFL1, PLFL2, PLFL3, PLFL4, PLFL5, PLFL6, PLFL7, PLFL8, PLFL9, PLFL10, PLFL11, PLFL12,PLFL13,PLFL14,PLFL15 } };
    /* Hit Direction */
    @FXML
    Text HDF1, HDF2, HDF3, HDF4, HDF5, HDF6, HDF7, HDF8, HDF9, HDHH1, HDHH2, HDHH3, HDHH4, HDHH5, HDHH6, HDHH7, HDHH8,
            HDHH9, HDRH1, HDRH2, HDRH3, HDRH4, HDRH5, HDRH6, HDRH7, HDRH8, HDRH9, HDWH1, HDWH2, HDWH3, HDWH4, HDWH5,
            HDWH6, HDWH7, HDWH8, HDWH9, HDSS1, HDSS2, HDSS3, HDSS4, HDSS5, HDSS6, HDSS7, HDSS8, HDSS9, HDS1, HDS2, HDS3,
            HDS4, HDS5, HDS6, HDS7, HDS8, HDS9, HDW1, HDW2, HDW3, HDW4, HDW5, HDW6, HDW7, HDW8, HDW9, HDHBP1, HDHBP2,
            HDHBP3, HDHBP4, HDHBP5, HDHBP6, HDHBP7, HDHBP8, HDHBP9, HDLD1, HDLD2, HDLD3, HDLD4, HDLD5, HDLD6, HDLD7,
            HDLD8, HDLD9, HDGD1, HDGD2, HDGD3, HDGD4, HDGD5, HDGD6, HDGD7, HDGD8, HDGD9, HDFL1, HDFL2, HDFL3, HDFL4,
            HDFL5, HDFL6, HDFL7, HDFL8, HDFL9;

    Text[][] HD = { { HDF1, HDF2, HDF3, HDF4, HDF5, HDF6, HDF7, HDF8, HDF9 },
            { HDHH1, HDHH2, HDHH3, HDHH4, HDHH5, HDHH6, HDHH7, HDHH8, HDHH9 },
            { HDRH1, HDRH2, HDRH3, HDRH4, HDRH5, HDRH6, HDRH7, HDRH8, HDRH9 },
            { HDWH1, HDWH2, HDWH3, HDWH4, HDWH5, HDWH6, HDWH7, HDWH8, HDWH9 },
            { HDSS1, HDSS2, HDSS3, HDSS4, HDSS5, HDSS6, HDSS7, HDSS8, HDSS9 },
            { HDS1, HDS2, HDS3, HDS4, HDS5, HDS6, HDS7, HDS8, HDS9 },
            { HDW1, HDW2, HDW3, HDW4, HDW5, HDW6, HDW7, HDW8, HDW9 },
            { HDHBP1, HDHBP2, HDHBP3, HDHBP4, HDHBP5, HDHBP6, HDHBP7, HDHBP8, HDHBP9 },
            { HDLD1, HDLD2, HDLD3, HDLD4, HDLD5, HDLD6, HDLD7, HDLD8, HDLD9 },
            { HDGD1, HDGD2, HDGD3, HDGD4, HDGD5, HDGD6, HDGD7, HDGD8, HDGD9 },
            { HDFL1, HDFL2, HDFL3, HDFL4, HDFL5, HDFL6, HDFL7, HDFL8, HDFL9 } };
    /* Cumulative-Bottom */
    @FXML
    Text CF1, CF2, CF3, CF4, CF5, CF6, CF7, CF8, CF9, CF10;
    Text[] CF = { CF1, CF2, CF3, CF4, CF5, CF6, CF7, CF8, CF9, CF10 };
    // the array array array
    Text[][][] mag = { NP, CT, PT, PL,HD,};

    protected void cleardata() {// not finished
        for (Text text : CF) {
            //text.setText("void");
        }
        for (Text[][] txtuu : mag) {
            for (Text[] txtu : txtuu) {
                for (Text txt : txtu) {
                    //txt.setText("void");
                }
            }
        }
    }

    protected void displaydata() {
        realdisplay();
    }

    public void DataAdd(ActionEvent actionevent) {
        gotoinput();
    }

    public void gotoabout(ActionEvent actionevent) {
        gotoabout();
    }

    public void Refresh(ActionEvent actionevent) {
        open(ListOTeam.getValue());
        debug();
        displaydata(); 
    }

    public void initialize(URL arg0, ResourceBundle arg1) {
        stage.setOnCloseRequest(e -> closesafe());
        loadteamname();
        addTeamListtobox(ListOTeam);
        cleardata(); 

    }

    private void realdisplay(){
        System.out.println("Here:");
        debug();
//I removed the calculations area because it exceeded the 30000 character limit in Stack overflow
    }

}

我删除了计算,因为它们占用了大量空间,并且不会以任何方式编辑 HashMap 。

我对可能导致问题的原因有一些想法,但尝试改变其中任何一个只会让问题变得更糟。

仅供引用,该程序与棒球计算有关。

任何想法都有帮助。先感谢您。

最佳答案

Data类中,您将每个HashMap(BallsStrikes等)初始化为空 HashMap ,然后将 arr 初始化为 HashMap 的数组。在 open 方法中,我认为这一行是从序列化文件加载 HashMap 数组的位置:

    arr = (HashMap[]) ois.readObject();

该行丢弃了 arr 引用的旧数组,将其替换为文件中的映射数组。它不会改变实例变量BallsStrikes等。它们仍然引用您首先创建的空 map 。如果您希望它们引用加载的 map ,则需要以与之前相反的方式复制引用:

    Balls = arr[0];
    Strikes = arr[1];

等等。

关于java - HashMap 数组未反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42837876/

相关文章:

java - equals() 可以判断发送的对象和收到的对象实际上是否相同?

android - 如何用逗号分隔数组列表值并放入hashmap

java - 在Java游戏中实现声音

php - 在单个 SQL 行中存储多个值

c# - 如何使用 Json.Net 将所有空值序列化为空字符串

Java的HashMap冲突解决

Java HashMap 哈希函数

java - 在项目中用java显示图像

java - 用 Java 将控制台输出写入文件

java - 如何使用 Java 查询和过滤多个嵌套数组