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

2014/4/27

Error..

1
2
mcGreen mcGreen

2014/4/27

#
When I kill one of the terrorist in my game, I get an error but I do not see the exact cause. java.lang.NullPointerException at Player.bopHead(Player.java:32) at Player.platformAbove(Player.java:73) at Player.act(Player.java:15) 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.NullPointerException at Player.bopHead(Player.java:32) at Player.platformAbove(Player.java:73) at Player.act(Player.java:15) 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.IllegalArgumentException: java.io.FileNotFoundException: Could not find file: TerroristR9.png at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:293) at greenfoot.GreenfootImage.<init>(GreenfootImage.java:109) at Terrorist.<init>(Terrorist.java:20) at Wolfbase.initiateLevel(Wolfbase.java:33) at Wolfbase.<init>(Wolfbase.java:17) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at greenfoot.core.Simulation.newInstance(Simulation.java:581) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:409) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:468) at greenfoot.core.Simulation.maybePause(Simulation.java:281) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205) Caused by: java.io.FileNotFoundException: Could not find file: TerroristR9.png at greenfoot.util.GreenfootUtil.getURL(GreenfootUtil.java:537) at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:290) ... 14 more java.lang.IllegalArgumentException: java.io.FileNotFoundException: Could not find file: TerroristR9.png at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:293) at greenfoot.GreenfootImage.<init>(GreenfootImage.java:109) at Terrorist.<init>(Terrorist.java:20) at Wolfbase.initiateLevel(Wolfbase.java:33) at Wolfbase.<init>(Wolfbase.java:17) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at greenfoot.core.Simulation.newInstance(Simulation.java:581) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:409) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:468) at greenfoot.core.Simulation.maybePause(Simulation.java:281) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205) Caused by: java.io.FileNotFoundException: Could not find file: TerroristR9.png at greenfoot.util.GreenfootUtil.getURL(GreenfootUtil.java:537) at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:290) ... 14 more 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.getOneObjectAtOffset(Actor.java:867) at Enemy.moveAround(Enemy.java:30) at Terrorist.act(Terrorist.java:35) 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.getOneObjectAtOffset(Actor.java:867) at Enemy.moveAround(Enemy.java:30) at Terrorist.act(Terrorist.java:35) 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.getOneObjectAtOffset(Actor.java:867) at Enemy.moveAround(Enemy.java:30) at Terrorist.act(Terrorist.java:35) 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.getOneObjectAtOffset(Actor.java:867) at Enemy.moveAround(Enemy.java:30) at Terrorist.act(Terrorist.java:35) 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.getOneObjectAtOffset(Actor.java:867) at Enemy.moveAround(Enemy.java:30) at Terrorist.act(Terrorist.java:35) 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.getOneObjectAtOffset(Actor.java:867) at Enemy.moveAround(Enemy.java:30) at Terrorist.act(Terrorist.java:35) 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.getOneObjectAtOffset(Actor.java:867) at Enemy.moveAround(Enemy.java:30) at Terrorist.act(Terrorist.java:35) 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)
Zamoht Zamoht

2014/4/27

#
As the error says "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." This means that you are trying to use the terrorist after he has been removed from the world. You will have to provide us with some code.
erdelf erdelf

2014/4/27

#
in addition to zamoht TerroristR9.png isnt in the images folder and in the player class in line 32 you try to access something not there
mcGreen mcGreen

2014/4/27

#
The thing is, R9 is not being accessed in the code. Is there no way to spoiler (hide) the code so it wont block the whole page immediantly? Player:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Player extends Actor
{
    private int xSpeed = 3;
    private int vSpeed = 0;
    private int acceleration = 1;
    private int jumpStrength = 15;
    private int direction = 1;
    public int shootingCounter = 10;  
    private GreenfootImage runr1 = new GreenfootImage ("PlayerR1.png");
    private GreenfootImage runr2 = new GreenfootImage ("PlayerR2.png");
    private GreenfootImage runr3 = new GreenfootImage ("PlayerR3.png");
    private GreenfootImage runr4 = new GreenfootImage ("PlayerR4.png");
    private GreenfootImage runr5 = new GreenfootImage ("PlayerR5.png");
    private GreenfootImage runr6 = new GreenfootImage ("PlayerR6.png");
    private GreenfootImage runr7 = new GreenfootImage ("PlayerR7.png");
    private GreenfootImage runr8 = new GreenfootImage ("PlayerR8.png");
    private GreenfootImage runl1 = new GreenfootImage ("PlayerL1.png");
    private GreenfootImage runl2 = new GreenfootImage ("PlayerL2.png");
    private GreenfootImage runl3 = new GreenfootImage ("PlayerL3.png");
    private GreenfootImage runl4 = new GreenfootImage ("PlayerL4.png");
    private GreenfootImage runl5 = new GreenfootImage ("PlayerL5.png");
    private GreenfootImage runl6 = new GreenfootImage ("PlayerL6.png");
    private GreenfootImage runl7 = new GreenfootImage ("PlayerL7.png");
    private GreenfootImage runl8 = new GreenfootImage ("PlayerL8.png");
    private int frame = 1;
    public void act() 
    {
        checkFall();
        checkKeys();
        checkDirection();
        platformAbove();
        checkRightWalls();
        checkLeftWalls();
        shooting();
        shootingCounter --;
    }
    
    public void checkDirection()
    {
        if (!Greenfoot.isKeyDown("right") && direction == 1)
        {
            setImage("PlayerR.png");
        }
        
        if (!Greenfoot.isKeyDown("left") && direction == -1)
        {
            setImage("PlayerL.png");
        }
        
        if(direction == 1 && !onPlatform())
        {
            setImage("JumpR.png");
        }
        
         if(direction == -1 && !onPlatform())
        {
            setImage("JumpL.png");
        }
    }
    
    public void animateRight()
    {
        if(frame == 4)
        {
            setImage(runr1);
        }
        else if(frame == 8)
        {
            setImage(runr2);
        }
        else if(frame == 12)
        {
            setImage(runr3);
        }
        else if(frame == 16)
        {
            setImage(runr4);
        }
        else if(frame == 20)
        {
            setImage(runr5);
        }
        else if(frame == 24)
        {
            setImage(runr6);
        }
        else if(frame == 28)
        {
            setImage(runr7);
        }
        else if(frame == 32)
        {
            setImage(runr8);
            frame = 2;
        }
        frame ++;
    }
    
     public void animateLeft()
    {
        if(frame == 4)
        {
            setImage(runl1);
        }
        else if(frame == 8)
        {
            setImage(runl2);
        }
        else if(frame == 12)
        {
            setImage(runl3);
        }
        else if(frame == 16)
        {
            setImage(runl4);
        }
        else if(frame == 20)
        {
            setImage(runl5);
        }
        else if(frame == 24)
        {
            setImage(runl6);
        }
        else if(frame == 28)
        {
            setImage(runl7);
        }
        else if(frame == 32)
        {
            setImage(runl8);
            frame = 2;
        }
        frame ++;
    }
    
    public void stayOnGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        
        setLocation(getX(), newY);
    }
    
     public void bopHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
        
        setLocation(getX(), newY);
    }
    
     public boolean onPlatform()
    {
       int spriteHeight = getImage().getHeight()/2;
       Actor ground = getOneObjectAtOffset(0, spriteHeight, Platform.class);
       if(ground == null)
       {
           return false;
       }
       else
       {
           stayOnGround(ground);
           return true;
       }
    }
    
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
    
    public void jump()
    {
        vSpeed = -jumpStrength;
        fall();
    }
    
      public boolean platformAbove()
    {
        int spriteHeight = getImage().getHeight()/2;
       Actor ceiling = getOneObjectAtOffset(0, -spriteHeight, Platform.class);
       if(ceiling != null)
       {
           vSpeed = 1;
           bopHead(ceiling);
           return true;
       }
       else
       {
           return false;
       }
    }
  
     public boolean checkRightWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/2);
        Actor rightWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
        if(rightWall == null)
        {
            return false;
        }
        else
        {
            stopByRightWall(rightWall);
            return true;
        }
    }

    public void stopByRightWall(Actor rightWall)
    {
        int wallWidth = rightWall.getImage().getWidth();
        int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2;
        setLocation(newX - 5, getY());

    }
    
    public boolean checkLeftWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/-2);
        Actor leftWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
        if(leftWall == null)
        {
            return false;
        }
        else
        {
            stopByLeftWall(leftWall);
            return true;
        }
    }

    public void stopByLeftWall(Actor leftWall)
    {
        int wallWidth = leftWall.getImage().getWidth();
        int newX = leftWall.getX() + (wallWidth + getImage().getWidth())/2;
        setLocation(newX + 5, getY());
    }
    
    public boolean shooting()
    {
        if(Greenfoot.isKeyDown("space") && shootingCounter <= 0 && direction ==1)
        {
            getWorld().addObject(new ShotR(), getX() + 16, getY() - 2);
            Greenfoot.playSound("AkShot.mp3");
            shootingCounter = 20;
            return true;
        }
        
        if(Greenfoot.isKeyDown("space") && shootingCounter <= 0 && direction ==-1)
        {
            getWorld().addObject(new ShotL(), getX() - 16, getY() - 2);
            Greenfoot.playSound("AkShot.mp3");
            shootingCounter = 20;
            return true;
        }
        return false;
    }
    
    public void checkFall()
    {
        if(onPlatform())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
   
    public void checkKeys()
    {        
        if (Greenfoot.isKeyDown("left"))
        { 
          move(-xSpeed);
          direction = -1;
          animateLeft();       
        }
        
        if (Greenfoot.isKeyDown("right"))
        {
           move(xSpeed);
           direction = 1;
           animateRight();
        }
        
        if (Greenfoot.isKeyDown("up") && onPlatform())
        {
            jump();
        }
    }
}
       
Terrorist:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Terrorist extends Enemy
{
    int health = 4;
    private GreenfootImage runr1 = new GreenfootImage ("TerroristR1.png");
    private GreenfootImage runr2 = new GreenfootImage ("TerroristR2.png");
    private GreenfootImage runr3 = new GreenfootImage ("TerroristR3.png");
    private GreenfootImage runr4 = new GreenfootImage ("TerroristR4.png");
    private GreenfootImage runr5 = new GreenfootImage ("TerroristR5.png");
    private GreenfootImage runr6 = new GreenfootImage ("TerroristR6.png");
    private GreenfootImage runr7 = new GreenfootImage ("TerroristR7.png");
    private GreenfootImage runr8 = new GreenfootImage ("TerroristR8.png");
    private GreenfootImage runl1 = new GreenfootImage ("TerroristL1.png");
    private GreenfootImage runl2 = new GreenfootImage ("TerroristL2.png");
    private GreenfootImage runl3 = new GreenfootImage ("TerroristL3.png");
    private GreenfootImage runl4 = new GreenfootImage ("TerroristL4.png");
    private GreenfootImage runl5 = new GreenfootImage ("TerroristL5.png");
    private GreenfootImage runl6 = new GreenfootImage ("TerroristL6.png");
    private GreenfootImage runl7 = new GreenfootImage ("TerroristL7.png");
    private GreenfootImage runl8 = new GreenfootImage ("TerroristL8.png");
    private int frame = 1;
    public void act() 
    {
        checkFall();
        checkDamageShotR();
        checkDamageShotL();
        die();
        moveAround();
        checkDirection();
    } 
    
    public void animateRight()
    {
        if(frame == 4)
        {
            setImage(runr1);
        }
        else if(frame == 8)
        {
            setImage(runr2);
        }
        else if(frame == 12)
        {
            setImage(runr3);
        }
        else if(frame == 16)
        {
            setImage(runr4);
        }
        else if(frame == 20)
        {
            setImage(runr5);
        }
        else if(frame == 24)
        {
            setImage(runr6);
        }
        else if(frame == 28)
        {
            setImage(runr7);
        }
        else if(frame == 32)
        {
            setImage(runr8);
            frame = 1;
        }
        frame ++;
    }
    
    public void animateLeft()
    {
        if(frame == 4)
        {
            setImage(runl1);
        }
        else if(frame == 8)
        {
            setImage(runl2);
        }
        else if(frame == 12)
        {
            setImage(runl3);
        }
        else if(frame == 16)
        {
            setImage(runl4);
        }
        else if(frame == 20)
        {
            setImage(runl5);
        }
        else if(frame == 24)
        {
            setImage(runl6);
        }
        else if(frame == 28)
        {
            setImage(runl7);
        }
        else if(frame == 32)
        {
            setImage(runl8);
            frame = 1;
        }
        frame ++;
    }
    
    public void checkDirection()
    {
        if(xSpeed < 0)
        {
            animateLeft();
        }
        else
        {
            animateRight();
        }
    }
    
     public boolean checkDamageShotL()
    {
        if(getOneIntersectingObject(ShotL.class) != null)
        {
            getWorld().removeObject(getOneIntersectingObject(ShotL.class));
            health --;
            Greenfoot.playSound("Hit1.mp3");   
            return true;
        }
        else
        {
            return false;
        }   
    }

     public boolean checkDamageShotR()
    {
        if(getOneIntersectingObject(ShotR.class) != null)
        {
            getWorld().removeObject(getOneIntersectingObject(ShotR.class));
            health --;
            Greenfoot.playSound("Hit1.mp3");   
            return true;
        }
        else
        {
            return false;
        }   
    }
    
      public boolean die()
    {
        if(health <= 0)
        {
            death();
            return true;
        }
        else
        {
            return false;
        }
    }
 }
danpost danpost

2014/4/27

#
Please clear your terminal window (from the menubar -- "Options>Clear"), reproduce the error and post the new error message here. If you have made major changes to the Player class within the last half hour, repost it also.
mcGreen mcGreen

2014/4/27

#
Ah! Ok now the error is (almost) clear. I didnt know that it also showed past (?) errors which made identifying the problem pretty difficult for me. Could you help me fix it? 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.getOneObjectAtOffset(Actor.java:867) at Enemy.moveAround(Enemy.java:30) at Terrorist.act(Terrorist.java:35) 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) Edit: Here is the "moveAround" method:
public void moveAround()
    {
       Actor ground = getOneObjectAtOffset(lookForEdge, lookForGroundDistance, Platform.class); 
       if(ground == null)
       {
           xSpeed *= -1;  
           lookForEdge *= -1;
       }
       else
       {
           move(xSpeed);
       }
    }
danpost danpost

2014/4/27

#
This line: at Enemy.moveAround(Enemy.java:30) says that the error occurred in your 'moveAround' method at line '30' in your 'Enemy' class. Is line 30 the 'move' line or the 'getOneObjectAtOffset' line?
mcGreen mcGreen

2014/4/27

#
Its this one
Actor ground = getOneObjectAtOffset(lookForEdge, lookForGroundDistance, Platform.class);
danpost danpost

2014/4/27

#
Ok. In your Terrorist class you have this as your act method:
public void act() 
{
    checkFall();
    checkDamageShotR();
    checkDamageShotL();
    die();
    moveAround();
    checkDirection();
}
Line 6 could possibly remove the terrorist from the world. At that point, you can no longer use any Actor class methods that require the actor be in the world ('getX', 'getIntersectingObjects', etc.) or access your world fields and method by way of 'getWorld' (as it will now return a 'null' value). Options: you can rearrange the order of calls in the act method or place the following line after any method call that could possibly have your actor removed from the world:
if (getWorld() == null) return;
mcGreen mcGreen

2014/4/27

#
Oh wow now I see, a stupid mistake, I didnt know the order in the act method is important, but if you think about it it IS kind of obvious! Thanks a bunch! NOW my last question. The animation does not work a 100%, for some reason a sprite (a jumping sprite) is in the walking animation, but it should not be there. Do you have any ideas why this could be? The source code is already posted but if you want I can upload the game so you can take a look.
mcGreen mcGreen

2014/4/27

#
Ignore cannot delete
danpost danpost

2014/4/27

#
mcGreen wrote...
The source code is already posted but if you want I can upload the game so you can take a look.
Where is the source code posted?
mcGreen mcGreen

2014/4/27

#
The code I posted is most of it already. Ill upload the game in a second. Edit: Here it is. Seems to slow down when played here, it runs normal on my computer tho.
danpost danpost

2014/4/27

#
Well, I do not have at least the world class or the Enemy, Platform, ShotL, and ShotR class codes available (that I know of). Also, of what class is your 'jumping sprite' (or at least what should be a 'jumping sprite')?
mcGreen mcGreen

2014/4/27

#
danpost wrote...
Also, of what class is your 'jumping sprite' (or at least what should be a 'jumping sprite')?
With "jumping sprite" I simply ment the playersprite shown while jumping.
There are more replies on the next page.
1
2