Asked By
Nick Howard
360 points
N/A
Posted on - 05/28/2011
Hi everyone!
I am currently working on a JAVA project and i have a problem. I have tried to run a file and got this error message
exception in thread "main" java.lang.OutOfMemoryError:Java
i think i have done everything right, i have gone through the code again and again, edited where possible, but still i get the same error.
I have spent quite a lot of time in this and the deadline for the project submission is just around the corner. I believe there are Java gurus in here and i can find assistance.
Here is my 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){
}
On which section or line do you think the error could be and how do i fix it? I will greatly appreciate any help / suggestion or idea. Thanks in advance.
Nick.
Answered By
Harry
200 points
N/A
#107733
I need help! Java exception error
Hi Nick,
The exception in thread "main" java.lang.OutOfMemoryError: Java occurs because java virtual machine runs out of memory.
When java program is executed its variables, objects and values are stored in the memory of the java virtual machine.
Then after some time java garbage collector will randomly execute and clean the memory for further usage of the programs.
But if a program creates more objects in memory in a very quick time, the memory will be overloaded.
If that happens there will be no more space for java interpreter to create new instances so it will throw an out of memory exception.
The main problem of your code is you got a never ending loop inside your while loop. Check the logic in there. You have assigned the variable l as 0.
But you never increment it. And why do you increment the i after the while loop(i++;). There is no use of incrementing it after the while loop.
As a solution to your problem you should have increment the variable I inside the scope of the while loop. The following code will give you the exact solution I’m described.
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++;
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){
}
I hope your problem will be solved. I will give some information about java loops later. Also if you use an IDE for java development it will be easier for you.
It will reduce the time and effort you will spend on creating a software program. And it will automatically identify the compile errors of your code. So see you soon.
Answered By
Harry
200 points
N/A
#107735
I need help! Java exception error
As you know loop is a block of code which iterate according to a specific condition. In your case the wile loop is always executes its block of code when its condition is true.
The specialty of the while loop is it checks the condition at the beginning. If the condition is not become false the loop will never terminate.
While (Condition) {
}
There is another variation of the while loop. It is called the do-while loop.
The difference between while and do-while is the position of the condition.
Do-while loop checks its condition at the end of the loop. So the do block of the loop executes once before checking the condition.
If you want some part of code to be run before checking the condition, better use the do-while loop.
Do {
}
While ();
‘For’ loop is another type of loop which you can use for your program.
It has three parts within the brackets such as the variable initialization, condition and the increment. It will do the same job which the while loop has done.
For (Variable Initialization; Condition; Iteration) {
}
So I hope you will have a better idea about java loops after reading this brief introduction.