Reputation: 15
I am trying to rewrite a file after someone's won or lost a game of blackjack. Every time I run the game and win, I get this error:
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at User.setWinnings(User.java:278)
at Blackjack.addWinnings(Blackjack.java:182)
at Blackjack.showWinMessage(Blackjack.java:323)
at Blackjack.showOutcome(Blackjack.java:308)
at Blackjack.actionPerformed(Blackjack.java:401)
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$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
This is the code for the setWinnings method
public void setWinnings(int winnings2) {
winnings = winnings2;
IO.openInputFile("users.txt");
String line = IO.readLine();
StringBuilder newFile = new StringBuilder();
while(line != null){
String tokens[] = line.split("%");
if(tokens.length > 0){
if(tokens[0].equalsIgnoreCase(this.getUserID())){
String newLine = tokens[0] + tokens[1]+ tokens[2] + winnings2 + tokens[4]+ tokens[5]+ tokens[6];
newFile.append(newLine);
newFile.append("\n");
} else {
newFile.append(line);
newFile.append("\n");
}
}
}
IO.closeInputFile();
IO.createOutputFile("users.txt");
IO.println(newFile.toString());
IO.closeOutputFile();
}
I've already tried going into control panel and changing whatever that field was called under java to -Xms2048m or something. Any ideas why this won't work?
Edit: Note that the size of users.txt is only 82 bytes.
Upvotes: 0
Views: 780
Reputation: 756
You assign value to the line
variable only once, at a declaration stage: String line = IO.readLine();
and sin'ceit happens before the while
loop, inside the loop you allways use the same first line
without reassigning it with new lines from the input file.
In other words, the while
loop runs infinitely, or until the memory runs out, as happened in your case.
You have to add line = IO.readLine();
just before the ending of the while loop scope.
Upvotes: 0
Reputation: 86754
You have a loop
while(line != null) {
...
}
But within the loop you never read another line. The loop never terminates and you keep appending the same data to newFile
. Eventually you consume all of memory.
Upvotes: 3