2013-07-23 123 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