2013-07-23 174 views
0

我正在制作一个简单的基于文本的游戏,您必须键入命令才能执行某些操作。我最近在游戏中添加了一个功能,可以让您保存进度。但由于某种原因,如果您尝试将游戏保存在现有的保存文件上,则会崩溃。这里是保存游戏的代码(当它无法保存它说:“有我在尝试保存游戏数据游戏现在将关闭一个错误。”像预期):Java程序覆盖现有的.txt文件时会崩溃吗?

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) 
+0

请复制从命令行启动的堆栈跟踪。 – Phani

回答

0

似乎所有的GameSave方法都是静态的,保存为closeFile方法。然而所有参考相同的领域。尝试使closeFile静态。毕竟,它似乎引用了静态字段。

当然,就像其他方法一样,如:GameSave.closeFile,而不是someInstanceOfGameSave.closeFile

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

编辑

确保您在closeFile方法检查空为好。

+0

将其更改为静态后,仍然给我出错。 –

+0

非常感谢您的帮助,我设法解决它,谢谢你! –

+0

@MohsenMaesumi欢迎你 – Mena

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

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

希望这有助于!

+0

谢谢你的帮助,问题已经解决:) –

+0

好吧,没问题:)只是稍微澄清一下如何阅读错误和应用它们都是:) – user2277872