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

2017/10/30

Error while creating objects of Actor class

Sowmya02 Sowmya02

2017/10/30

#
Error: 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. is received in the line - getWorld().(new Object2(), getX(),getY()); of class Object2(). Can you please help me out in creating objects of class Object2
import greenfoot.*;
import java.util.*;  
public class Enemy extends Actor
{
   
    private int targetx=0, targety=0,jeda=0;
    public static int dir=0;
    private int vx=1,lives=50,x,y;
    public boolean toRemove=false;
    //MainEnemy em=null
    
    public void checkwalls()
    {
            if(isAtEdge()&&dir==0)
              {
                  setLocation(getX(),getY()-50);
                  dir=1;
                  
                }
                if(isAtEdge()&&dir==1)
              {
                  setLocation(getX(),getY()+50);
                  dir=0;
                  
                }
            
    }
    public void destroy()
    {
        getWorld().addObject(new B(),getX(),getY());
        remEnemy();
        if(getLives()==0)
        toRemove=true;
        else
        toRemove=false;
       
    }
    public void remEnemy()
    {
        lives--;
    }
    public int getLives()
    {
      
        return lives;
    }
    
    public void move()
    {
        if(dir==0)
        {
        setLocation(getX(),getY()+vx);
        checkwalls();
    }
    if(dir==1)
    {
        setLocation(getX(),getY()-vx);
        checkwalls();
    }
    Strategy s2 = new Object2();
    Strategy s3 = new Object3();
    s2.setSuccessor(s3);
    s2.fly("ob2");
   
    }
    public void act() 
    {
        
        if(!toRemove) move();
        else
        getWorld().removeObject(this);
        
        
        
    }    
}

public class Object2 extends Actor implements Strategy 
{
    private Strategy successor=null;
    public void act() 
    {
       // fly();
    }   
    public void setSuccessor(Strategy s)
    {
        this.successor = s;
    }
    public void fly(String f)
    {
       if(f.equalsIgnoreCase("ob2"))
        
            getWorld().(new Object2(), getX(),getY());
     }
}

public interface Strategy 
{
    public void act(); 
    public void fly(String f);
    public void setSuccessor(Strategy s);
}



danpost danpost

2017/10/30

#
You have two problems (at least). The one causing the error is this: the 'fly' method cannot be executed on an Object2 actor that is not in any world; yet, you are calling it (see line 63) on 's2', which you had just created (line 60) but has not been added to a world. This brings us to the other problem, which is line 93. I presume the missing method name was to be 'addObject'; however, the Object2 actor created on that line does not have a successor (which I presume you wanted the one added into the world to have). The methods 'getX' and 'getY' cannot be called while the actor is not in any world (causing the error) and the 'getWorld' method will return a 'null' value (which, upon calling a method on it, will throw a NullPointerException. You could add the 's2' object into the world at line 63 (replacing the call to 'fly' there). I cannot be certain as to the location in the world at which you want to add it, however (maybe where the Enemy object is).
You need to login to post a reply.