-2

When I load this file, I always get the NullPointerException. I try many thing and I could not find a way to fix it. When I saved I used the Game object.

     @Override
        public void load(Superhero aHero,Villain aVillain) throws IOException, ClassNotFoundException {

        ObjectInputStream ois = null;
        FileInputStream fis = null;
        File f = new File("C:\\prog24178\\dcgames.dat");
        Game aGame = new Game(aHero,aVillain);
        try {
            fis = new FileInputStream(f);
            ois = new ObjectInputStream(fis);
            while(true){
                aGame = (Game) ois.readObject();
                System.out.println(aGame);

            }
        } catch(EOFException eof){
            ois.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DcGameController.class.getName()).log(Level.SEVERE, null, ex);
        } 

    }

When I open the file I see something like that:

    ’ sr 
    model.Game      ^r Z gameOverZ heroTurnL theHerot Lmodel/Superhero;L 
    theVillaint Lmodel/Villain;xp  sr model.hero.Batman      ^r Z armedL sidekickq ~ xr model.Superhero      ^r I energyZ secretIdentityL codenamet Ljava/lang/String;L hometownq ~ xr model.Avatar      ^r 
    I agilityI     enduranceI fightingI   hitPointsI  intuitionI psycheI reasonI strengthL     firstNameq ~ L lastNameq ~ xp            W            t  q ~      t Batmant Gotham Citysq ~                                 t Dickt Grayson    ppsr model.villain.Cheetah      ^r  xr 
    model.Villain      ^r I energyZ insaneL codenameq ~ xq ~             b            t   Priscillat Rich    t Cheetah

So I know it's working but I would like to read and after that I could set the setting I need to use.

That's my save method :

    @Override
    public void saveGame(Game aGame) {
        File aSaveFile = new File("C:\\prog24178\\dcgames.dat");
        FileOutputStream fos = null;

        if (!aSaveFile.getParentFile().exists()) {
            aSaveFile.getParentFile().mkdir();
        }
        try {
            if (!aSaveFile.exists()) {
                aSaveFile.createNewFile();
            }
            if (aSaveFile.length() > 0) {
                fos = new FileOutputStream(aSaveFile, true);
            } else {
                fos = new FileOutputStream(aSaveFile, false);
            }
            if (oos == null) {
                oos = new ObjectOutputStream(fos);
            }
            oos.writeObject(aGame);
        } catch (FileNotFoundException ex) {

        } catch (Exception x) {
            System.out.println("Something bad happened" + x);
        }
    }

Thanks for helping me to fix my issue.

1 Answers1

0

I think there 2 problems u should check,

1.fos = new FileOutputStream(aSaveFile, true);
if aSaveFile already has some data and u append another object data to aSaveFile, its data will be confused, u'll not be able to bring the object back.

2.oos.writeObject(aGame);
when u finish writeObject(), u should flush the stream buffer using oos.flush();