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

2023/4/28

So many errors

Edit Edit

2023/4/28

#
Is Greenfoot not able to handle many actors? 50% of the time my scenario has more then 40 actors and multiple actors have to be removed at the same time I will get errors. Maybe greenfoot multi-threading issue? Mostly: java.lang.ArithmeticException: / by zero at greenfoot.ActorSet.resizeHashmap(ActorSet.java:87) at greenfoot.ActorSet.remove(ActorSet.java:174) at greenfoot.ActorSet.remove(ActorSet.java:149) at greenfoot.TreeActorSet.remove(TreeActorSet.java:185) at greenfoot.World.removeObject(World.java:458) Exception in thread "Timer-2" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 9 at greenfoot.ActorSet.resizeHashmap(ActorSet.java:88) at greenfoot.ActorSet.add(ActorSet.java:68) at greenfoot.TreeActorSet.add(TreeActorSet.java:180) at greenfoot.World.addObject(World.java:430) java.lang.StackOverflowError at greenfoot.collision.ibsp.BSPNode.containsActor(BSPNode.java:205) at greenfoot.collision.ibsp.IBSPColChecker.insertObject(IBSPColChecker.java:220) at greenfoot.collision.ibsp.IBSPColChecker.insertObject(IBSPColChecker.java:257) java.lang.NullPointerException at greenfoot.World.getObjectsAtPixel(World.java:771) at greenfoot.WorldVisitor.getObjectsAtPixel(WorldVisitor.java:72) at greenfoot.core.WorldHandler.getObject(WorldHandler.java:244) at greenfoot.core.WorldHandler$3.getTopMostActorAt(WorldHandler.java:393) at greenfoot.gui.input.mouse.MouseEventData.setActors(MouseEventData.java:315) at greenfoot.gui.input.mouse.MousePollingManager.newActStarted(MousePollingManager.java:177) at greenfoot.core.WorldHandler.startSequence(WorldHandler.java:635) at greenfoot.core.WorldHandler.simulationChangedSync(WorldHandler.java:673) at greenfoot.core.Simulation.fireSimulationEventSync(Simulation.java:746) at greenfoot.core.Simulation.runOneLoop(Simulation.java:495) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) AND greenfoot.ActorRemovedFromWorld even though I surround the entire act method with if (getWorld() != null) and also the line before getWorld().removeObject(this) with if (getWorld() != null) Also actors sometimes become invisible and frozen when removed and sometimes an actor will be moved to (0, 0) rather than disappearing from the world. (I found a old issue on the GitHub repository which is the same as this but it was apparently fixed(?))
Edit Edit

2023/4/28

#
It's more like 80% of the time actually. I don't think its a issue with my code because the scenario sometimes runs fine.
danpost danpost

2023/4/28

#
Edit wrote...
even though I surround the entire act method with if (getWorld() != null)
That does no good. The act method will only begin if the actor is in the world to begin with. In other worlds, it is checked by greenfoot before the act method is called.
and also the line before getWorld().removeObject(this) with if (getWorld() != null)
getWorld is only one of many method that require the actor be in the world. Almost every 'get' method in the Actor class requires it (some, if not all, exceptions are getImage and getRotation).
Also actors sometimes become invisible and frozen when removed and sometimes an actor will be moved to (0, 0) rather than disappearing from the world. (I found a old issue on the GitHub repository which is the same as this but it was apparently fixed(?))
Well, if you could provide some of your codes, maybe we could find something that might help. Even your error trace output seems incomplete (if has nothing pointing to your code specifically; yet, I am sure the trace should show something there).
Edit Edit

2023/4/28

#
danpost wrote...
Edit wrote...
even though I surround the entire act method with if (getWorld() != null)
That does no good. The act method will only begin if the actor is in the world to begin with. In other worlds, it is checked by greenfoot before the act method is called.
and also the line before getWorld().removeObject(this) with if (getWorld() != null)
getWorld is only one of many method that require the actor be in the world. Almost every 'get' method in the Actor class requires it (some, if not all, exceptions are getImage and getRotation).
Also actors sometimes become invisible and frozen when removed and sometimes an actor will be moved to (0, 0) rather than disappearing from the world. (I found a old issue on the GitHub repository which is the same as this but it was apparently fixed(?))
Well, if you could provide some of your codes, maybe we could find something that might help. Even your error trace output seems incomplete (if has nothing pointing to your code specifically; yet, I am sure the trace should show something there).
Each time I get a error it seems to be one of those 5 and it happens randomly so I'm pretty sure its something wrong with Greenfoot The entire error code for the first error is
java.lang.ArithmeticException: / by zero
    at greenfoot.ActorSet.resizeHashmap(ActorSet.java:87)
    at greenfoot.ActorSet.remove(ActorSet.java:174)
    at greenfoot.ActorSet.remove(ActorSet.java:149)
    at greenfoot.TreeActorSet.remove(TreeActorSet.java:185)
    at greenfoot.World.removeObject(World.java:458)
    at Zombie.deathAnim(Zombie.java:70)
    at Zombie.act(Zombie.java:47)
    at greenfoot.core.Simulation.actActor(Simulation.java:567)
    at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
    at greenfoot.core.Simulation.runContent(Simulation.java:193)
    at greenfoot.core.Simulation.run(Simulation.java:183)
This seems to happen randomly whenever there is a large amount of Actors in the world and I do not have any division in my code. I think the error is at line 87 in ActorSet.resizeHashmap where it does
int hash = seq % numActors;
My Code for Zombie class:
public void act()
{
    if (getWorld() != null) {
     
        if (isLiving()) {
            update();
             
        } else {
         deathAnim();            
        }
    }
}
 
public void update() {
     
}
 
public void deathAnim() {
    if (!resetAnim) {
            frame = 0;    
            resetAnim = true;
    }
    if (frame <=6) {
        if (finalDeath) {
            if (!fixAnim) {
                fixAnim = true;
                AudioPlayer.play(80, "zombie_falling_1.mp3", "zombie_falling_2.mp3");
                 
                MyWorld.addObject(new fallingZombie(fall), getX()-10, getY()+20);
                if (getWorld() != null)
                    getWorld().removeObject(this);
                return;
            }
             
             
        } else {
            if (!spawnHead) {
                spawnHead = true;
                AudioPlayer.play(80, "limbs_pop.mp3");
                getWorld().addObject(new Head(), getX(), getY()-10);
            }
            if (!eating) {
                animate(headless, 350, false);
                move(-walkSpeed);
            } else {
                animate(headlesseating, 350, false);
            }
             
        }
    } else if (!finalDeath) {
        resetAnim = false;
        finalDeath = true;
         
        for (ArrayList<Zombie> i : MyWorld.level.zombieRow) {
            if (i.contains(this)) {
                i.remove(this);                    
                break;
            }
        }
         
    } 
 
}
I do not have the error codes for the other errors because they are not happening anymore (Even though I did not change any code??) but I believe they all point to the Zombie class getWorld().removeObject(this) line.
Edit Edit

2023/4/28

#
I have fixed the issue. If you call addObject from a TimerTask class (java.util.Timer), then Greenfoot has a chance of giving an error when that Actor is being removed along with other ones. The only way to avoid the errors is to stop using the java.util.Timer. Greenfoot is not thread safe it seems
You need to login to post a reply.