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

2014/1/8

I need Help - NullPointerException

DoctorrBoss DoctorrBoss

2014/1/8

#
Whenever I shoot my ships I want them to respawn, so I added the code form the Joy ofCode videos for respawning. It works until I shoot my alien ship, and it says NullPointerException, and shows the error on the last line of code(add object, and stuff) WHAT DO I DO???
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class AlienDroneShip here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AlienDroneShip extends Enemy
{
    /**
     * Act - do whatever the AlienDroneShip wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveDownwards();
        explodeIfPotatoHits();
    } 

    public void moveDownwards()

    {
        int x = getX();  
        int y = getY();  
        int ny = getY()+1;  
        setLocation(x,ny) ;  
    }

    public void explodeIfPotatoHits()

    { if(canSee(Potato.class))
        {
            getWorld().removeObject(this);
            spawnNewShip();
        }
    }

    public void spawnNewShip()
    {
        {
            AlienDroneShip newAlienDroneShip;
            newAlienDroneShip = new AlienDroneShip();

            World world;
            world = getWorld();

            int worldWidth = 480;
            int worldHeight = 800;

            int x = Greenfoot.getRandomNumber(worldWidth);
            int y = Greenfoot.getRandomNumber(worldHeight);

            world.addObject(newAlienDroneShip, x-200, y);
        }

    }
}
bourne bourne

2014/1/8

#
Swap the order of these two: getWorld().removeObject(this); spawnNewShip();
erdelf erdelf

2014/1/8

#
u are removing the ship before u spawn another ship, swap line 34 and 35 and it should work Edit: sry bourne, we was writing at the same time
DoctorrBoss DoctorrBoss

2014/1/8

#
Yes!!!!!!!!!!!!!!!! Thank You Sooo much :)))))) :DDDDD
DoctorrBoss DoctorrBoss

2014/1/8

#
okay one more question
{
    private Counter counter;
    
    public alienShip(Counter pointCounter)
    {
        counter = pointCounter;
    }

    /**
     * Act - do whatever the alienShip wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveDownwards();
        explodeIfPotatoHits();
    } 

    public void moveDownwards()

    {
        int x = getX();  
        int y = getY();  
        int ny = getY()+1;  
        setLocation(x,ny) ;  
    }

    public void explodeIfPotatoHits()

    { 
       AlienBoss newBoss;
       newBoss = new AlienBoss();
        
      if(canSee(Potato.class))
        {
            spawnNewShip();
            getWorld().removeObject(this);
            counter.add(10);
        }
        
      if (counter.getValue()>=100)
        {
            getWorld().addObject(newBoss,400, 50);
        }
    }

    public void spawnNewShip()
    {
        {
            alienShip newShip;
            newShip = new alienShip();

            World world;
            world = getWorld();

            int worldWidth = 480;
            int worldHeight = 400;

            int x = Greenfoot.getRandomNumber(worldWidth);
            int y = Greenfoot.getRandomNumber(worldHeight);

            world.addObject(newShip, x, y-200);
        }
    }

    public void gameOver()
    {
        Greenfoot.playSound("fanfare.wav");
        Greenfoot.stop();
    }
}
That is my code. I have added a counter. But now when I say newAlienShip in the spawnNewShip part, it comes with error like cannot be applied to give types. WHAT DO I DO??
bourne bourne

2014/1/8

#
Why are there double brackets for method spawnNewShip? And by convention, your class alienShip should start with a capital letter.
DoctorrBoss DoctorrBoss

2014/1/8

#
constructer in alienShip in class alienShip cannot be applied to given types; required: Counter; found:no arguements; reason:actual and formal argument lists differ in length
bourne bourne

2014/1/8

#
The only way to construct an alienShip is by: public alienShip(Counter pointCounter) so when you provide this, the default constructor that takes no arguments no longer exists. So you cannot do this: newShip = new alienShip();
DoctorrBoss DoctorrBoss

2014/1/8

#
what can I do instead? Do I add the public method you wrote?
bourne bourne

2014/1/8

#
You could pass it a new Counter. But are you creating alienShips elsewhere using the given constructor? If so, are you just passing it a new Counter each time? If yes you could just do the following:
private Counter counter =  new Counter(); // Or however you construct a Counter  

public alienShip()
{
    
}
DoctorrBoss DoctorrBoss

2014/1/8

#
kk thanks!!
You need to login to post a reply.