0

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.

M. Amadou
  • 15
  • 4
  • Possible duplicate of [What is an OutOfMemoryError and how do I debug and fix it](http://stackoverflow.com/questions/24510188/what-is-an-outofmemoryerror-and-how-do-i-debug-and-fix-it) – Raedwald Jan 17 '16 at 23:50

2 Answers2

3

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.

Jim Garrison
  • 81,234
  • 19
  • 144
  • 183
0

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.

theDima
  • 666
  • 4
  • 10