Frage

I'm making a simple text based game where you have to type commands to preform certain actions. I recently added a feature to the game that allows you to save your progress. But for some reason if you try to save your game over an existing save file it crashes. Here is the code that saves the game (when it fails to save it says "There was an error when trying to save game data. The game will now close." like expected):

import java.util.Formatter;
import javax.swing.JOptionPane;

public class Gamesave {
    private static Formatter gamesave;
    private static Formatter firstTimeSave;
    private static Formatter attackpoints;
    private static Formatter defensepoints;
    private static Formatter skillpoints;
    private static Formatter wins;
    private static Formatter loses;
    private static Formatter money;
    // Attackpoints, defensepoints, skillpoints, wins, loses, money
    public static void openFile(){
        try{
            attackpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_attackpoints.txt");
            defensepoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_defensepoints.txt");
            skillpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_skillpoints.txt");
            wins = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_wins.txt");
            loses = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_loses.txt");
            money = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_money.txt");
            gamesave = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+".txt");
            firstTimeSave = new Formatter("c:\\FightNight\\Game Data\\firstTimeSave.txt");
        }catch (Exception e) {JOptionPane.showMessageDialog(null, "There was an error when trying to save game data. The game will now close."); System.exit(0);}

    }

    public static void addRecords(){
        attackpoints.format("%s",MainClass.attackpoints);
        defensepoints.format("%s",MainClass.defensepoints);
        skillpoints.format("%s",MainClass.skillpoints);
        wins.format("%s",MainClass.wins);
        loses.format("%s",MainClass.loses);
        money.format("%s",MainClass.money);
        firstTimeSave.format("%b", MainClass.firstTime);

    }

    public void closeFile(){
        attackpoints.close();
        defensepoints.close();
        skillpoints.close();
        wins.close();
        loses.close();
        money.close();
        gamesave.close();
        firstTimeSave.close();
    }

}

Here is the code that calls the classes:

static class SaveAction implements ActionListener{
        public void actionPerformed (ActionEvent e){
            try{
                Gamesave.openFile();
                Gamesave.addRecords();
                save.closeFile();
                JOptionPane.showMessageDialog(null, "Your game has been saved.");
            }catch (Exception e1) {JOptionPane.showMessageDialog(null, "Sorry, that is an invalid response.");}
        }
    }

Another note, when the game is launched for the first time on a computer it creates the directories for the save files and anything else needed. Thank you for any help!

The stack trace:

java.io.FileNotFoundException: c:\FightNight\Saves\null\null_attackpoints.txt (The system cannot find the path specified)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.util.Formatter.<init>(Unknown Source)
    at Gamesave.openFile(Gamesave.java:16)
    at CommandLine$SaveAction.actionPerformed(CommandLine.java:93)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
War es hilfreich?

Lösung

It seems all your GameSave methods are static, save for the closeFile method. However all reference the same fields. Try make closeFile static. It seems to reference static fields after all.

And of course, call it like the other methods, as in: GameSave.closeFile, not someInstanceOfGameSave.closeFile.

Finally, if that doesn't work, add the line e.printStackTrace(); before showing your message dialog and print the resulting stack trace as an edit of your question.

Edit

Make sure you check for nulls in your closeFile method as well.

Andere Tipps

It is because you have nulls in your file path. Notice the first line of your error stack

 java.io.FileNotFoundException: c:\FightNight\Saves\**null\null**_attackpoints.txt (The system cannot find the path specified)

There is no possible way that you can have a null in the file path name. So, with that being said, you need to go back and fix the objects that are containing the information in this part of the code. Also notice this:

at Gamesave.openFile(Gamesave.java:16)

This line tells you exactly where the error is..

So, lets check that method...

 try{
        attackpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_attackpoints.txt");
        defensepoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_defensepoints.txt");
        skillpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_skillpoints.txt");
        wins = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_wins.txt");
        loses = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_loses.txt");
        money = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_money.txt");
        gamesave = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+".txt");
        firstTimeSave = new Formatter("c:\\FightNight\\Game Data\\firstTimeSave.txt");
    }catch (Exception e) {JOptionPane.showMessageDialog(null, "There was an error when trying to save game data. The game will now close."); System.exit(0);}

I'm not seeing a way that this class accesses the static class "MainClass"'s newProfileName variable....so there is most likely the reason as to why you are not getting the proper information in the file name..

I'd say to update the newProfileName inside the try/catch block, to keep it up to date...

something like so

  try{

        MainClass.newProfileName = //accessed information from whereever you get your new profile name in the Code....
        attackpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_attackpoints.txt");
        defensepoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_defensepoints.txt");
        skillpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_skillpoints.txt");
        wins = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_wins.txt");
        loses = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_loses.txt");
        money = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_money.txt");
        gamesave = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+".txt");
        firstTimeSave = new Formatter("c:\\FightNight\\Game Data\\firstTimeSave.txt");
    }catch (Exception e) {JOptionPane.showMessageDialog(null, "There was an error when trying to save game data. The game will now close."); System.exit(0);}

The system is getting a null in the file name, which cannot be. Null means it is nothing, an d has no directory position...so find a way to update that variable, and that should fix it...

Hope this helps!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top