Results 1 to 3 of 3
  1. #1
    Rasenganboy16 is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default PLease help. The error is: exception in thread "main" java.lang.OutOfMemoryError:Java

    heap space.

    My codes:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class board implements ActionListener{
    	JButton y[]=new JButton[1000000];
    	int xa, ya, i, l;
    	JFrame Dave;
    	
    	public static void main(String[] args){
    		board chuy = new board();
    		chuy.setFrame();
    	}
    	
    	public void setFrame(){
    		xa=0;
    		ya=0;
    		l=0;
    		i=1;
    		Dave = new JFrame("Board");
    		Dave.setLayout(new GraphPaperLayout(new Dimension(1000,1000)));
    		while(l<9999){
    			y[i]=new JButton("");
    			Dave.add(y[i], new Rectangle(xa,ya,1,1));
    			if(xa<=1000){
    				if(xa==1000){
    					xa=0;
    					ya++;
    				}
    				else xa++;  
    			}
    		}
    			i++;
    		
    		Dave.setVisible(true);
    		Dave.setSize(360,400);
    		Dave.setLocation(100,100);
    		Dave.setResizable(false);
    		Dave.setDefaultCloseOperation(Dave.DISPOSE_ON_CLOSE);
    		}
    		
    		public void actionPerformed(ActionEvent e){
    		
    		}	
    }
    Last edited by Fubarable; 05-24-2011 at 07:26 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Have you tried giving your program more available heap memory on the command line (or via your IDE if using one)?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    A comment on coding style: your button array would be more representative if declared as a two-dimensional array, and the while(...) loop would be more expressive coded as nested for(...) loops.
    Java Code:
      JButton[][] buttons = new JButton[1000][1000];
      :
      :
        for (int x = 0; x < 1000; x++) {
          for (int y = 0; y < 1000; y++) {
            buttons[x][y] = new JButton();
            dave.add(buttons[x][y], new Rectangle(x, y, 1, 1));
          }
        }
    Better still would be to declare a static final variable SIDE (or WIDTH and HEIGHT if these are not always to be equal) and use the variable/s in the layout parameters, button array declaration and loop limits. Then if you want to change from 1000 X 1000 to some other numbers, that change is in only one place.

    db

Similar Threads

  1. Replies: 5
    Last Post: 06-22-2010, 04:51 PM
  2. Replies: 4
    Last Post: 06-03-2010, 09:40 AM
  3. Replies: 1
    Last Post: 11-20-2009, 08:58 PM
  4. Replies: 3
    Last Post: 04-22-2009, 09:25 PM
  5. Replies: 14
    Last Post: 06-12-2008, 09:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •