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

2018/1/13

Getting Error - " Java heap space"

Spicy0506 Spicy0506

2018/1/13

#
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
69
70
public class Leaf extends Actor
{
    /*  Fields Go Here */
    public static int numLeaves;
    int timer;
    int dx;
    boolean fallingRight;
    /*  This is a Constructor */
    public Leaf()
    {
        switch( Greenfoot.getRandomNumber(3) )
        {
            case 0: setImage("Leaf1.png"); break;
            case 1: setImage("Leaf2.png"); break;
            case 2: setImage("Leaf3.png"); break;
        }
        setRotation((int)Math.random()*360);
        timer =(int)(Math.random()*900)+100;
        if ((int)(Math.random()*5)>=3)
        {
            fallingRight=true;
        }
        else if ((int)(Math.random()*5)<=3)
        {
        }
        numLeaves++;
    }  
 
    public void act()
    {
        timer--;
        if (timer==0 & timer==-1)
        {
            setLocation (getX()+dx/2,getY()+5-Math.abs(dx)/4);
            if(fallingRight == true)
            {
                dx++;
                if (dx > 20)
                {
                    fallingRight = false;
                }
            }
            else if(fallingRight == false)
            {
                dx--;
                if (dx < -20)
                {
                    fallingRight = true;
                }
            }
            if (getY() > 600)
            {
                numLeaves--;
                getWorld().removeObject(this);
            }
        }
    }   
 
     
    public static int getNumLeaves()
    {
        return numLeaves;
    }
 
    public static void reset()
    {
        numLeaves = 0;
    }
      
}
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
public class Tree extends World
{
 
    public Tree()
    {   
        super(1000, 750, 1);
        Leaf.reset();
    }
 
    public void addLeaves( int amount )
    {
        for(int i=0;0 < amount; i++)
        {
            int x = (int)(Math.random()*800)+100;
            int y = (int)(Math.random()*400);
            Leaf leaves = new Leaf();
            addObject (leaves,x,y);
        }
    }
 
    public void increaseLeavesTo( int amount )
    {
       while (Leaf.numLeaves < amount)
        {
            int x = (int)(Math.random()*800)+100;
            int y = (int)(Math.random()*400);
            Leaf leaves = new Leaf();
            addObject (leaves,x,y);
        }
    }
}
danpost danpost

2018/1/13

#
It is impossible to tell why you are getting that error with what is given. All that happens when you create a world is you set 'numLeaves' in the Leaf class to zero. With no actors in the world and no act method in the Tree class, nothing can happen when the scenario is started. By itself, adding a few leaves to the world should not cause that kind on error; so, it does not appear that the given code is suspect to the cause.
Spicy0506 Spicy0506

2018/1/13

#
Spicy506 Spicy506

2018/1/13

#
OK, so for some reason my account is not allowed to post at the moment because I have a post pending approval from the site administrators. *So I made a new account real quick..... Here is what comes up in the terminal window java.lang.OutOfMemoryError: Java heap space at greenfoot.ActorSet.resizeHashmap(ActorSet.java:77) at greenfoot.ActorSet.add(ActorSet.java:62) at greenfoot.TreeActorSet.add(TreeActorSet.java:172) at greenfoot.World.addObject(World.java:419) at Tree.addLeaves(Tree.java:19) at __SHELL0.run(__SHELL0.java:9) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at greenfoot.localdebugger.LocalDebugger$QueuedExecution.run(LocalDebugger.java:277) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:502) at greenfoot.core.Simulation.maybePause(Simulation.java:305) at greenfoot.core.Simulation.runContent(Simulation.java:218) at greenfoot.core.Simulation.run(Simulation.java:211) java.lang.OutOfMemoryError: Java heap space at java.util.AbstractCollection.toArray(AbstractCollection.java:136) at java.util.ArrayList.<init>(ArrayList.java:177) at greenfoot.core.Simulation.runOneLoop(Simulation.java:555) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) Exception in thread "D3D Screen Updater" java.lang.OutOfMemoryError: Java heap space at java.lang.reflect.Array.newArray(Native Method) at java.lang.reflect.Array.newInstance(Array.java:75) at java.util.Arrays.copyOf(Arrays.java:3212) at java.util.ArrayList.toArray(ArrayList.java:407) at sun.java2d.d3d.D3DScreenUpdateManager.run(D3DScreenUpdateManager.java:433) at java.lang.Thread.run(Thread.java:745) Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space at greenfoot.Actor.calcBounds(Actor.java:590) at greenfoot.Actor.getBoundingRect(Actor.java:546) at greenfoot.World.getObjectsAtPixel(World.java:752) at greenfoot.WorldVisitor.getObjectsAtPixel(WorldVisitor.java:67) at greenfoot.core.WorldHandler.getObject(WorldHandler.java:404) at greenfoot.core.WorldHandler.getObject(WorldHandler.java:352) at greenfoot.platforms.ide.WorldHandlerDelegateIDE.mouseMoved(WorldHandlerDelegateIDE.java:356) at greenfoot.core.WorldHandler.mouseMoved(WorldHandler.java:981) at greenfoot.gui.input.InputManager.mouseMoved(InputManager.java:317) at java.awt.Component.processMouseMotionEvent(Component.java:6578) at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3342) at java.awt.Component.processEvent(Component.java:6302) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4538) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method)
davmac davmac

2018/1/13

#
Spicy506 wrote...
OK, so for some reason my account is not allowed to post at the moment because I have a post pending approval from the site administrators. *So I made a new account real quick.....
Please don't do that. As you can see it didn't work anyway - your post in the new account also had to be approved. One account per person, please. This second account will remain disabled.
Spicy0506 Spicy0506

2018/1/13

#
davmac wrote...
Spicy506 wrote...
OK, so for some reason my account is not allowed to post at the moment because I have a post pending approval from the site administrators. *So I made a new account real quick.....
Please don't do that. As you can see it didn't work anyway - your post in the new account also had to be approved. One account per person, please. This second account will remain disabled.
Sorry about that....but going back to the problem I figured it out
1
2
3
4
5
6
7
for(int i=0;0 < amount; i++)
        {
            int x = (int)(Math.random()*800)+100;
            int y = (int)(Math.random()*400);
            Leaf leaves = new Leaf();
            addObject (leaves,x,y);
        }
I had to change the "0 < amount" to "i < amount" simpler mistake but thank you both for the help
danpost danpost

2018/1/13

#
Spicy0506 wrote...
I figured it out < Code Omitted > I had to change the "0 < amount" to "i < amount" simpler mistake
Simple, yet easy to miss (as apparent I had done). Sorry. Glad you found it.
You need to login to post a reply.