I am just starting out with Greenfoot so bear with me. I am working through the Greenfoot book. One of the programs in the book is a crab/worm/lobster scenario. I am trying to make the program work such that if there are no worms left in the world, the program will end, but I get a null pointer when I try to add a crab. Any help is awesome!
My worm class is empty.
Crab Class
CrabWorld
import greenfoot.*;
/**
* This class defines a crab. Crabs live on the beach.
*/
public class Crab extends Actor
{
public int count = 0;
public int wormCount = ((CrabWorld)getWorld()).getCount();
public void act()
{
turnOnEdge();
this.keyboardControl();
this.lookForWorm();
isOver();
}
public void lookForWorm()
{
if(isTouching(Worm.class))
{
removeTouching(Worm.class);
count++;
}
}
public int getCount()
{
return count;
}
public void randomTurn()
{
if(Greenfoot.getRandomNumber(100)<10)
{
turn(Greenfoot.getRandomNumber(90)-45);
}
}
public void turnOnEdge()
{
if(isAtEdge())
{
turn(17);
}
}
public void keyboardControl()
{
if(Greenfoot.isKeyDown("left"))
{
setRotation(180);
move(5);
}
if(Greenfoot.isKeyDown("right"))
{
setRotation(0);
move(5);
}
if(Greenfoot.isKeyDown("up"))
{
setRotation(270);
move(5);
}
if(Greenfoot.isKeyDown("down"))
{
setRotation(90);
move(5);
}
}
public int isOver()
{
if(count == wormCount)
{
Greenfoot.stop();
}
return wormCount;
}
}
import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage)
public class CrabWorld extends World
{
public int count = 0;
/**
* Create the crab world (the beach). Our world has a size
* of 560x560 cells, where every cell is just 1 pixel.
*/
public CrabWorld()
{
super(560, 560, 1);
}
public int getCount()
{
for(Object worm : getObjects(Worm.class))
{
count++;
}
return count;
}
}
