This site requires JavaScript, please enable it in your browser!
Greenfoot back
sparrow72
sparrow72 wrote ...

2012/7/25

java.lang.OutOfMemoryError: Java heap space

sparrow72 sparrow72

2012/7/25

#
these are the errors I keep on getting: Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space Exception in thread "RMI TCP Connection(3)-127.0.0.1" java.lang.OutOfMemoryError: Java heap space Exception in thread "RMI RenewClean-" java.lang.OutOfMemoryError: Java heap space Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: Java heap space I have several menu actors all with the same code except with different names, my question is how to avoid the heap space error, and is it a code problem or something else?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Levels here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Levels extends Buttons
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    /**
     * Create a new counter, initialised to 0.
     */
    public Levels()
    {
        background = getImage();  // get image from class
        setImage("invisoButton.png");
         
        updateImage();
    }
 
    /**
     * Act - do whatever the Levels wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        click();
 
    }   
    public void click()
    {
 
        if (Greenfoot.mouseClicked(this))
        {
 
            getWorld().removeObjects(getWorld().getObjects(Controls.class));
            getWorld().removeObjects(getWorld().getObjects(Credits.class));
            getWorld().removeObjects(getWorld().getObjects(Settings.class));
 
            Level1 level1 = new Level1();
            getWorld().addObject(level1, 100, 375);
 
            Ready ready = new Ready();
            getWorld().addObject(ready, 500, 375);
 
            Back back = new Back();
            getWorld().addObject(back, 100, 375);
 
            setLocation(175, 75);
 
        }   
    }
 
    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("Levels", 44, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2,(image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
 
}
Thanks! :)
danpost danpost

2012/7/26

#
Chances are, there is a problem with your code; however, it is probably not in this class, but, maybe in your world class. Also, is there not more to the error message you are getting? I somehow cannot believe that what you posted was the entire message and it helps to show all of it.
gusbus123 gusbus123

2012/7/26

#
it could probrably be cause your code for creating the object is repeatedly adding the same object over and over again in the same spot. This would make u run out of memory as there are too many actors within the world for your computer to keep it running. Not in this part of code but probs others.
sparrow72 sparrow72

2012/7/26

#
the actual error was continuing to add to its self for about 10min before i just copied it all and deleted repeat lines I also think that the adding the same object over and over was the problem, because I finally did notice it, but forgot to look in the menu class (the only thing that the background makes, and it constructs the entire menu) had everything it was suppose to create in the act method. Thanks for the help guys! =D
sparrow72 sparrow72

2012/7/26

#
ok so I fixed everything but now I am getting a new error I have never seen before,
1
java.lang.NullPointerException
the error is stated on the line
1
getWorld().addObject(levels, 175, 75);
I looked for any null anything and nothing is. so how do i fix it?
danpost danpost

2012/7/26

#
The object that spawns 'levels' is not in the world. You either already had it removed or, more probably, you put the line in question in a constructor, where the object has not yet been added to the world (or you just forgot to add the object into the world, which I doubt would be the case).
sparrow72 sparrow72

2012/7/26

#
ok so how come when I have
1
public class Level1 extends Levels
and
1
getWorld().removeObjects(getWorld().getObjects(Levels.class));
in side of 'Levels' right after creating 'Level1' it deletes the 'Level1'?
danpost danpost

2012/7/26

#
When you delete all objects of a class, you are also deleting all objects that are sub-classes of that class. Since Level1 extends Levels, Level1 is an instance of Levels, but more specifically, an instance of Level1. I have never tried anything like this, but maybe you can use a List method to remove 'this' within the parameter?
You need to login to post a reply.