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

2020/4/20

Null pointer exception

scaps scaps

2020/4/20

#
having this error when pressing z to create the projectile class from another actor class. "java.lang.NullPointerException at Guy.shoot(Guy.java:57) at Guy.act(Guy.java:25) 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)" here is part of the code from the actor launching the projectile and the actual projectile.
public void act() 
    {
        
        
        movement();
        if(Greenfoot.isKeyDown("z")){
            shoot();
        }

private void shoot(){
            
            if(delay != 0){
            delay--;
        }
            //defines world and returns it to this actor
            
             //defines projectile and returns it to this actor
            if(delay == 0) {
            
            world.addObject(projectile,100,100);
            delay= 70;
        
        }
public void act() 
    {
        move(1);

    }  
Super_Hippo Super_Hippo

2020/4/20

#
Change ‘world’ in line 20 to ‘getWorld()’. You might want to change ‘projectile’ to ‘new Projectile()’ as well.
You need to login to post a reply.