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

2012/7/15

Error within Terminal Window

Marie Marie

2012/7/15

#
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); } }
SPower SPower

2012/7/15

#
It is at this line: at Food.updateImage(Food.java:50) the 50th line in the Food class. This is the 'error': java.lang.NullPointerException A null pointer exception occors when you use an object which is null, like this:
1
2
MyActor a = null;
a.dosomething(); // a is null: crash
This is the line which does it all:
1
image.fillOval(locX, locY,IMG_SIZE,IMG_SIZE);
because image is null:
1
private GreenfootImage image = null;
you first have to do something like:
1
image = new GreenfootImage(WIDTH, HEIGHT);
I hope you understand, but for more info, I'll post a course about exceptions soon (you're dealing with exceptions), so watch this page: http://www.greenfoot.org/collections/321 and look for 'Exceptions tutorial'. It will be there very soon! And next time: click on the code button underneath the text field when you're inserting code in a comment again :)
danpost danpost

2012/7/15

#
Actually, the error is due to the first line in your updateImage() method
1
new GreenfootImage(IMG_SIZE,IMG_SIZE);
which creates a GreenfootImage object, but does nothing with it. It, in essence, is lost as soon as it is created. Instead use
1
image =  new GreenfootImage(IMG_SIZE,IMG_SIZE);
which assigns it to your GreenfootImage field.
Marie Marie

2012/7/15

#
Perfect. Thanks, I got this working now. And it was also throwing the same error again in another class as I was making the same mistake. Thanks for the quick response!
SPower SPower

2012/7/20

#
The course is posted! Sorry for the time it took, I was busy with some other things. Look at this scenario: http://www.greenfoot.org/scenarios/5709
You need to login to post a reply.