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

2021/3/7

help i keep getting this msg

JCD JCD

2021/3/7

#
i keep getting this error (line 38) java.lang.NullPointerException at defaults.act(defaults.java:38) 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) heres my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class defaults here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class defaults extends Animal

{
    public int health = 50;
    
    private Counter counter;
    public defaults()
    {
        
    }
    public defaults(Counter pointCounter)
    {
        counter = pointCounter;
    }
    /**
     * Act - do whatever the defaults wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(), getY() + 1);
        
        if(canSee(bullet.class))
        {
            health = health - 1;
            
        }
        if (health == 0)
        {
            counter.add(5);
                        createNew();
            getWorld().removeObject(this);
           
        }
        if (health == 0)
        {
            counter.add(5);
        }
        
}
    private void createNew()
    {
        defaults defaults;
        defaults = new defaults();
        
        World world;
        world = getWorld();
        
        
        int x = Greenfoot.getRandomNumber(600);
        int y = 100;
        
        world.addObject(defaults, x, y);
    }
}
JCD JCD

2021/3/7

#
im making it so then when this enemy dies a new one spawns
danpost danpost

2021/3/7

#
Start by removing lines 15 thru 18. You will most probably get different errors that will lead you toward fixing it. Oh, and change line 52 to:
defaults = new defaults(counter);
(that is one you would have been led to)
You need to login to post a reply.