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

2016/4/14

NullPointerException when spawning objects

Hydrowarrior Hydrowarrior

2016/4/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Terrain here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Terrain extends Actor
{
    public boolean Slide = false;
    public static int TileThatDespawned;

    public Terrain(int TileNumber, int TileType)
    {
        if(TileType == 1){Road();setImage("Road.png");} else if (TileType == 2){Water();setImage("Water.png");} else if (TileType == 3) {Grass();setImage("Grass.png");}
    }

    public void act() 
    {     
        if(Slide == true){NeedsToSlide();}          
    }    

    public void NeedsToSlide()
    {   

        setLocation(getX(),getY()+5);
        if(getY() == 545){setLocation(getX(),getY()+5);}
        if(getY() > 598){getWorld().removeObject(this);SpawnTile();Froggy.TileFrogIsOn--;}
    }

    public void Road()
    {
    }

    public void Water()
    {
    }

    public void Grass()
    {
    }

    public void SpawnTile()
    {

        if(Froggy.TileFrogIsOn == 6)
        {
            Terrain terrain2 = new Terrain(2,Greenfoot.getRandomNumber(3));
            getWorld().addObject(terrain2, 400, 50);
        }
        if(Froggy.TileFrogIsOn == 5)
        {
            Terrain terrain3 = new Terrain(3,Greenfoot.getRandomNumber(3));
            getWorld().addObject(terrain3,400,50);
        }
        if(Froggy.TileFrogIsOn == 4)
        {
            Terrain terrain4 = new Terrain(4,Greenfoot.getRandomNumber(3));
            getWorld().addObject(terrain4,400,50);
        }
        if(Froggy.TileFrogIsOn == 3)
        {
            Terrain terrain5 = new Terrain(5,Greenfoot.getRandomNumber(3));
            getWorld().addObject(terrain5,400,50);
        }
        if(Froggy.TileFrogIsOn == 2)
        {
            Terrain terrain6 = new Terrain(6,Greenfoot.getRandomNumber(3));
            getWorld().addObject(terrain6,400,50);
        }

    }
}
Thats my entire code. The problem is in spawntile when it is trying to add the object. It returns NullPointerException. If I remove the code that spawns the objects in, everything works. Its very messy but its for a school project and I really don't want to start over :( Please help, thanks in advance!
danpost danpost

2016/4/14

#
You are removing the actor from the world before calling the 'SpawnTile' method (which requires the actor be in a world because you cannot call a method, like 'addObject' on 'getWorld' when 'getWorld' returns a 'null' value). Remove the act after calling 'SpawnTile', which can be simplified to this:
public void spawnTile() // method names should, by convention begin with a lowercase character
{
    Terrain terrain = new Terrain(8-Froggy.tileFrogIsOn, Greenfoot.getRandomNumber(3)); // the same goes with field names.
    getWorld().addObject(terrain, 400, 50);
}
You need to login to post a reply.