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

2022/9/19

I keep getting a null Pointer when tryng to get the location of a class

SliceOfCheese SliceOfCheese

2022/9/19

#
Im trying to make so a enemy I appear follows the player character, but when I try to get the location of the player so the enemy can turn towards the player and subsequently move towards him, it gives a null pointer. I have searched for answers in the forum, but when I implement them It just doesnt work. I still dont know why.
protected void lookAtUser()
    {
            Actor usuario= (Actor)myWorld.getUsuario();
            turnTowards(usuario.getX(), usuario.getY());
        
    }
This is my code right now, but I have changed it repeatedly and doesnt work. The main code that spawns the enemy are this:
private void anadirEnemigos()
    {
        if(Greenfoot.getRandomNumber(100)<1)
        {
            addObject(new MaleanteComun(), 400, 1);
        }
    }
This is the error: java.lang.NullPointerException at Enemigos.mirarAlUsuario(Enemigos.java:29) at Enemigos.<init>(Enemigos.java:17) at MaleanteComun.<init>(MaleanteComun.java:14) at MyWorld.anadirEnemigos(MyWorld.java:27) at MyWorld.act(MyWorld.java:20) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2022/9/19

#
The error occurs in the Enemigos class. Please provide the codes for that class.
SliceOfCheese SliceOfCheese

2022/9/19

#
public abstract class Enemigos extends Actor
{
    int vidas;
    MyWorld myWorld;
    
    
    public Enemigos(int vidas)
    {
        mirarAlUsuario();
        this.vidas = vidas;
        myWorld = (MyWorld)getWorld();
    }
    public void act()
    {
        
    }
    
    //Con esto nos encargamos de que el enemigo mire al usuario
    protected void mirarAlUsuario()
    {
            Actor player = (Actor)myWorld.getUsuario();
            int playerX = player.getX();
            int playerY = player.getY();
            turnTowards(playerX, playerY);
        
    }
    
    //eliminar las balas que entran en contacto con el enemigo, y bajarle la 
    //vida a este
    public void eliminarEnemigo()
    {
        if(vidas == 0)
        {
            getWorld().removeObject(this);
        }
    }
    
    public void golpeadoPorArma()
    {
        Actor actor = getOneIntersectingObject(Armas.class);
        
        if(actor != null)
        {   
            Armas arma = (Armas)actor;
            vidas -= arma.getDano();
            if(arma instanceof Proyectil)
            {
                getWorld().removeObject(actor);
            }
        }
    }
}
This is the code for the

Enemigos

class The MyWorld class is:
public class MyWorld extends World
{
    private Usuario usuario;
    public MyWorld()
    {    
        super(800, 600, 1); 
        prepare();
    }
    
    public void act()
    {
        anadirEnemigos();
    }
    
    private void anadirEnemigos()
    {
        if(Greenfoot.getRandomNumber(100)<1)
        {
            addObject(new MaleanteComun(), 400, 1);
        }
    }
    
    private void prepare()
    {   
        usuario = new Policia();
        addObject(usuario, 400, 400);
    }
    
    public Usuario getUsuario()
    {
        return usuario;
    }
}
SliceOfCheese SliceOfCheese

2022/9/19

#
I didnt thought that "Enemigos" would be so big, my bad, jajaja, I was tryng to put it cursive
danpost danpost

2022/9/19

#
SliceOfCheese wrote...
I didnt thought that "Enemigos" would be so big, my bad, jajaja, I was tryng to put it cursive
The problem is line 11 in the Enemigos class. That line is in the constructor block, which is executed before the actor can be placed into a world. Thus, the getWorld method must return a null value. So, when line 21 executed, a method is trying to be called on a null object (no World object is assigned to the myWorld field). Hence the error. Place (move) line 11 into a new overriding addedToWorld(World) method:
protected void addedToWorld(World world)
{
    myWorld = (MyWorld)world;
}
This method will be executed for an actor of the class when it is added into a world.
SliceOfCheese SliceOfCheese

2022/9/19

#
Men, thank you. Now it works. Thanks a lot <3
You need to login to post a reply.