0

我正在制作一个简单的基于文本的游戏,您必须在其中键入命令来执行某些操作。我最近在游戏中添加了一项功能,可让您保存进度。但是由于某种原因,如果您尝试将游戏保存在现有的保存文件上,它会崩溃。这是保存游戏的代码(当它保存失败时,它会说“尝试保存游戏数据时出错。游戏现在将关闭。”如预期的那样):

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();
    }

}

这是调用类的代码:

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.");}
        }
    }

另请注意,当游戏首次在计算机上启动时,它会为保存文件和其他所需内容创建目录。感谢您的任何帮助!

堆栈跟踪:

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)
4

2 回答 2

0

似乎你所有的GameSave方法都是静态的,除了closeFile方法。然而,所有引用相同的字段。尝试制作closeFile静态。毕竟它似乎引用了静态字段。

当然,像其他方法一样调用它,例如:GameSave.closeFile, not someInstanceOfGameSave.closeFile

最后,如果这不起作用,请e.printStackTrace();在显示您的消息对话框之前添加该行并将生成的堆栈跟踪打印为您的问题的编辑。

编辑

确保在你的closeFile方法中也检查空值。

于 2013-07-23T18:57:01.710 回答
0

这是因为您的文件路径中有空值。注意错误堆栈的第一行

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

文件路径名中不可能有空值。因此,话虽如此,您需要返回并修复包含这部分代码中信息的对象。还要注意这一点:

at Gamesave.openFile(Gamesave.java:16)

这一行告诉你错误的确切位置..

所以,让我们检查一下那个方法......

 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);}

我没有看到此类访问静态类“MainClass”的 newProfileName 变量的方式..所以很可能是您没有在文件名中获得正确信息的原因..

我想说更新 try/catch 块中的 newProfileName ,以使其保持最新...

像这样

  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);}

系统在文件名中得到一个空值,这是不可能的。Null 意味着它什么都不是,d 没有目录位置......所以找到一种方法来更新该变量,并且应该修复它......

希望这可以帮助!

于 2013-07-23T19:16:07.500 回答