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

2013/11/1

Error: Same codes different output!!

13111876 13111876

2013/11/1

#
Hey all :) I have 2 classes that are the same: 'Persoon' and 'Animal'. They have the same code but the class 'Animal' gives me error. CODE ANIMAL THIS IS THE CODE THAT GIVES ME AN ERROR. IT IS THE SAME AS PERSOON. (CODE BELOW)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Animal extends Actor
{
    public int weight;
    private int tempX;
    private int tempY;
    public void act() 
    {
        removeObjectAnimal();
        mouseInfo();
        followBoot();

    }
    
    public void followBoot()
    {    
        if(getOneIntersectingObject(Boat.class)!= null)
        {
            if (Greenfoot.isKeyDown("left") && getX() > 303) 
            {
                move(-5);
            }
            if (Greenfoot.isKeyDown("right") && getX() < 772) 
            {
                move(5);
            }
        }
    } 
  
    public void removeObjectAnimal()
    {
        if (getX()>=820)
        {
            getWorld().removeObject(this);
        }
    }

    public int getWeight()
    {
        return weight;
    }

    public void mouseInfo()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        {
            if (mouse == null)
            {
                return;
            }
            if(Greenfoot.mousePressed(this))
            {
                tempX = getX() - mouse.getX();
                tempY = getY() - mouse.getY();
            }
            if(Greenfoot.mouseDragged(this))
            {
                setLocation(mouse.getX()+tempX,mouse.getY()+tempY);
            }
        }

    }

}
---------------------------------------------------------------------------------------------------------- CODE PERSOON WORKS FINE!! IT IS THE SAME.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Collection;
public class Persoon extends Actor
{
    public int weight;
    private int tempX;
    private int tempY;
    public void act() 
    {
        removeObjectPersoon();
        mouseInfo();
        followBoot();
        
    }

    public void followBoot()
    {    
        if(getOneIntersectingObject(Boat.class)!= null)
        {
            if (Greenfoot.isKeyDown("left") && getX() > 303) 
            {
                move(-5);
            }
            if (Greenfoot.isKeyDown("right") && getX() < 772) 
            {
                move(5);
            }
        }
    } 
    
    public void removeObjectPersoon()
    {
        if (getX()>=820)
        {
           getWorld().removeObject(this);
        }
    }
    
    public int getWeight()
    {
        return weight;
    }
    
    public void mouseInfo()
    {

        MouseInfo mouse = Greenfoot.getMouseInfo();
        {
            if (mouse == null)
            {
                return;
            }
            if(Greenfoot.mousePressed(this))
            {
                tempX = getX() - mouse.getX();
                tempY = getY() - mouse.getY();
            }
            if(Greenfoot.mouseDragged(this))
            {
                setLocation(mouse.getX()+tempX,mouse.getY()+tempY);
            }

        }

    }
}
This is the error I get: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Animal.followBoot(Animal.java:17) at Dog.act(Dog.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Animal.followBoot(Animal.java:17) at Cat.act(Cat.java:19) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Animal.followBoot(Animal.java:17) at Pig.act(Pig.java:19) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) ---------------------------------------------------------------------------------------------------------- Do I have to change the image??
Gevater_Tod4711 Gevater_Tod4711

2013/11/1

#
The problem is that your method removeObjectAnimal() removes your Animal from the world. And after removing it you try to get information from the world (getOneIntersectingObject(...)). This problem also is in your Persoon class. Probably you don't get an error there because the persoon object never moves until his x position is greater than 820. Or you just get the exception from the Animal class before the exception from the Persoon class and so the programm stops before you get the next exception. To fix this problm you can check whether your actor is still in the world before you execute the followBoot method (your actor is not in the world when getWorld() returns null).
danpost danpost

2013/11/1

#
Will need to see one of the classes 'Pig', 'Cat' or 'Dog' to see what is happening.
13111876 13111876

2013/11/1

#
@danpost This is the class Dog.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Dog extends Animal
{  
    public Dog()
    {
        weight = 25;
    }
    
    public void act() 
    {
        removeObjectAnimal();
        mouseInfo();
        followBoot();
    } 
}
danpost danpost

2013/11/1

#
Move 'removeObjectAnimal' to the end of the act method.
13111876 13111876

2013/11/1

#
@danpost It works good now, thank you very much. I thought it was something difficult. Thanks a lot for ur quick response.
You need to login to post a reply.