Hi, I'm getting an error within the terminal window:
java.lang.NullPointerException
at Food.updateImage(Food.java:50)
at Food.<init>(Food.java:25)
at AntWorld.setup2(AntWorld.java:48)
at AntWorld.<init>(AntWorld.java:21)
I'm unsure where the issue is? Can you provide some guidence on identifying my error? Based on the content of the error I'm assuming it is within the Food class, or AntWorld, so I'm copying the code below. Any guidence would be greatly appreciated.
FOOD CLASS:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Random;
/**
* A pile of food for which ants will search.
* A new object consists of 100 crumbs.
*/
public class Food extends Actor
{
private int nCrumbs;
private static final int MAX_CRUMBS = 100; // number of crumbs in one food pile
private static final int IMG_SIZE = 30; // the height and width of a food object
private static final int CRUMB_SIZE = 3; // the radius of a single crumb
private GreenfootImage image = null;
/**
* Create a new food source.
* It will have a certain number of crumbs
*/
public Food()
{
nCrumbs = MAX_CRUMBS;
updateImage();
}
/**
* Update the image of this food
*/
// Initialize image variable (already declared) as a new Image object
// declare local variables x and y
// For-loop that counts up to the total number of crumbs in the Food object
// Pick a random x for the crumb's center--random, not Gaussian
// Pick a random y for the crumb's center--random, not Gaussian
// Draw a solid circle centered at x and y with diameter 3
public void updateImage()
{
new GreenfootImage(IMG_SIZE,IMG_SIZE);
int locX;
int locY;
for (int i = 0; i < nCrumbs; i++)
{
// X = 1 + GreenfootImage.getRandomNumber(getImage(getWidth()));
locX = 1 + Greenfoot.getRandomNumber(5);
locY = 1 + Greenfoot.getRandomNumber(5);
image.fillOval(locX, locY,IMG_SIZE,IMG_SIZE);
}
}
/**
* Get the image of this food
*/
public GreenfootImage getImage()
{
if (image == null)
{
updateImage();
}
return image;
}
/**
* Set the image
*/
public void setImage()
{
updateImage();
}
/**
* Decrease the number of crumbs available
* One ant takes one crumb from the Food pile
*/
// decrease the number of crumbs in the pile by one
// If there are no crumbs left in this object ...
// removeObject(this); // Why doesn’t this remove the Food object??
// otherwise....
// update image
public void takeSome()
{
if (nCrumbs <= 0)
{
getWorld().removeObject(this);
}
else
{
updateImage();
}
}
/**
* Act - do whatever the Food wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// No action code required. Food is inert.
}
}
ANTWORLD CLASS:
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* The world where ants live.
*
* @author Michael Kolling
* @version 1.1
*/
public class AntWorld extends World
{
public static final int SIZE = 640;
/**
* Create a new world. It will be initialised with a few ant hills
* and food sources
*/
public AntWorld()
{
super(SIZE, SIZE, 1);
setPaintOrder(Ant.class, Pheromone.class, AntHill.class, Food.class);
setup2();
}
/**
* Create world contents: one ant hill and food.
*/
public void setup1()
{
removeObjects(getObjects(null)); // remove all existing objects
addObject(new AntHill(70), SIZE / 2, SIZE / 2);
addObject(new Food(), SIZE / 2, SIZE / 2 - 260);
addObject(new Food(), SIZE / 2 + 215, SIZE / 2 - 100);
addObject(new Food(), SIZE / 2 + 215, SIZE / 2 + 100);
addObject(new Food(), SIZE / 2, SIZE / 2 + 260);
addObject(new Food(), SIZE / 2 - 215, SIZE / 2 + 100);
addObject(new Food(), SIZE / 2 - 215, SIZE / 2 - 100);
}
/**
* Create world contents: two ant hills and food.
*/
public void setup2()
{
removeObjects(getObjects(null)); // remove all existing objects
addObject(new AntHill(40), 546, 356);
addObject(new AntHill(40), 95, 267);
addObject(new Food(), 80, 71);
addObject(new Food(), 291, 56);
addObject(new Food(), 516, 212);
addObject(new Food(), 311, 269);
addObject(new Food(), 318, 299);
addObject(new Food(), 315, 331);
addObject(new Food(), 141, 425);
addObject(new Food(), 378, 547);
addObject(new Food(), 566, 529);
}
/**
* Create world contents: two ant hills and food.
*/
public void setup3()
{
removeObjects(getObjects(null)); // remove all existing objects
addObject(new AntHill(40), 576, 134);
addObject(new AntHill(40), 59, 512);
addObject(new Food(), 182, 84);
addObject(new Food(), 39, 308);
addObject(new Food(), 249, 251);
addObject(new Food(), 270, 272);
addObject(new Food(), 291, 253);
addObject(new Food(), 339, 342);
addObject(new Food(), 593, 340);
addObject(new Food(), 487, 565);
}
}

