crash - Java program crashing when it overwrites an existing .txt file? -
i'm making simple text based game have type commands preform actions. added feature game allows save progress. reason if try save game on existing save file crashes. here code saves game (when fails save says "there error when trying save game data. game close." 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 error when trying save game data. game 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 code calls 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, invalid response.");} } }
another note, when game launched first time on computer creates directories save files , else needed. thank help!
the stack trace:
java.io.filenotfoundexception: c:\fightnight\saves\null\null_attackpoints.txt (the system cannot find path specified) @ java.io.fileoutputstream.open(native method) @ java.io.fileoutputstream.<init>(unknown source) @ java.io.fileoutputstream.<init>(unknown source) @ java.util.formatter.<init>(unknown source) @ gamesave.openfile(gamesave.java:16) @ commandline$saveaction.actionperformed(commandline.java:93) @ javax.swing.abstractbutton.fireactionperformed(unknown source) @ javax.swing.abstractbutton$handler.actionperformed(unknown source) @ javax.swing.defaultbuttonmodel.fireactionperformed(unknown source) @ javax.swing.defaultbuttonmodel.setpressed(unknown source) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(unknown source) @ java.awt.component.processmouseevent(unknown source) @ javax.swing.jcomponent.processmouseevent(unknown source) @ java.awt.component.processevent(unknown source) @ java.awt.container.processevent(unknown source) @ java.awt.component.dispatcheventimpl(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.lightweightdispatcher.retargetmouseevent(unknown source) @ java.awt.lightweightdispatcher.processmouseevent(unknown source) @ java.awt.lightweightdispatcher.dispatchevent(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.window.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$000(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)
it seems gamesave
methods static, save closefile
method. reference same fields. try make closefile
static. seems reference static fields after all.
and of course, call other methods, in: gamesave.closefile
, not someinstanceofgamesave.closefile
.
finally, if doesn't work, add line e.printstacktrace();
before showing message dialog , print resulting stack trace edit of question.
edit
make sure check nulls in closefile
method well.
Comments
Post a Comment