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

2014/5/2

Null Pointer Exception Issue

Tommy99 Tommy99

2014/5/2

#
Hello. I've been trying to fix this Null Pointer Exception issue, but I can't find out what the problem is. Here's the terminal window code: java.lang.NullPointerException at Home.<init>(Home.java:7) at Maze.<init>(Maze.java:25) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at greenfoot.core.Simulation.newInstance(Simulation.java:581) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:409) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:468) at greenfoot.core.Simulation.maybePause(Simulation.java:281) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205) Here's the Home class code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public class Home extends Actor { int x2 = Greenfoot.getRandomNumber(getWorld().getWidth()-1); int y2 = Greenfoot.getRandomNumber(getWorld().getHeight()-1); public Home() { GreenfootImage img = new GreenfootImage(40,40); img.fill(); setImage(img); Move(); Move2(); } public void Move() { Actor player = getOneIntersectingObject(Player.class); if(player!=null && getX()==0 && getY()==0){ setLocation(x2, y2); } } public void Move2() { Actor player2 = getOneIntersectingObject(Player.class); if(player2!=null && getX()==x2 && getY()==y2){ setLocation(0, 0); } } } Here's the code for the Maze class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Maze here. * * @author (your name) * @version (a version number or a date) */ public class Maze extends World { /** * Constructor for objects of class Maze. * */ public Maze() { super(10, 10, 40); addFood(20); prepare(); addObject(new Player(), 0, 0); addObject(new Lizard(), getWidth()-1, getHeight()-1); addObject(new Home(), getWidth()-1, getHeight()-1); } public void addFood(int n) { for(int f=0; f<n; f++){ int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); addObject(new Food(), x, y); } } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Wall wall = new Wall(); addObject(wall, 1, 1); wall.setLocation(0, 0); wall.setLocation(1, 0); Wall wall2 = new Wall(); addObject(wall2, 1, 1); Wall wall3 = new Wall(); addObject(wall3, 0, 3); Wall wall4 = new Wall(); addObject(wall4, 1, 3); Wall wall5 = new Wall(); addObject(wall5, 2, 3); Wall wall6 = new Wall(); addObject(wall6, 2, 4); Wall wall7 = new Wall(); addObject(wall7, 2, 5); Wall wall8 = new Wall(); addObject(wall8, 1, 5); Wall wall9 = new Wall(); addObject(wall9, 0, 5); Wall wall10 = new Wall(); addObject(wall10, 0, 6); Wall wall11 = new Wall(); addObject(wall11, 0, 7); Wall wall12 = new Wall(); addObject(wall12, 1, 7); Wall wall13 = new Wall(); addObject(wall13, 3, 7); Wall wall14 = new Wall(); addObject(wall14, 2, 7); Wall wall15 = new Wall(); addObject(wall15, 4, 7); Wall wall16 = new Wall(); addObject(wall16, 2, 1); Wall wall17 = new Wall(); addObject(wall17, 3, 1); Wall wall18 = new Wall(); addObject(wall18, 4, 1); Wall wall19 = new Wall(); addObject(wall19, 4, 2); Wall wall20 = new Wall(); addObject(wall20, 4, 3); Wall wall21 = new Wall(); addObject(wall21, 4, 4); Wall wall22 = new Wall(); addObject(wall22, 4, 5); Wall wall23 = new Wall(); addObject(wall23, 5, 5); Wall wall24 = new Wall(); addObject(wall24, 5, 7); Wall wall25 = new Wall(); addObject(wall25, 6, 4); Wall wall26 = new Wall(); addObject(wall26, 6, 3); Wall wall27 = new Wall(); addObject(wall27, 6, 2); Wall wall28 = new Wall(); addObject(wall28, 6, 5); Wall wall29 = new Wall(); addObject(wall29, 6, 1); Wall wall30 = new Wall(); addObject(wall30, 6, 0); Wall wall31 = new Wall(); addObject(wall31, 8, 0); Wall wall32 = new Wall(); addObject(wall32, 7, 0); Wall wall33 = new Wall(); addObject(wall33, 9, 0); Wall wall34 = new Wall(); addObject(wall34, 9, 1); Wall wall35 = new Wall(); addObject(wall35, 9, 2); Wall wall36 = new Wall(); addObject(wall36, 8, 2); Wall wall37 = new Wall(); addObject(wall37, 8, 3); Wall wall38 = new Wall(); addObject(wall38, 8, 4); Wall wall39 = new Wall(); addObject(wall39, 8, 5); Wall wall40 = new Wall(); addObject(wall40, 8, 6); Wall wall41 = new Wall(); addObject(wall41, 8, 7); Wall wall42 = new Wall(); addObject(wall42, 7, 7); Wall wall43 = new Wall(); addObject(wall43, 5, 8); Wall wall44 = new Wall(); addObject(wall44, 5, 9); Wall wall45 = new Wall(); addObject(wall45, 7, 8); Wall wall46 = new Wall(); addObject(wall46, 7, 9); Wall wall47 = new Wall(); addObject(wall47, 0, 0); Wall wall48 = new Wall(); addObject(wall48, 0, 1); removeObject(wall); removeObject(wall47); removeObject(wall35); removeObject(wall34); removeObject(wall33); removeObject(wall32); removeObject(wall31); removeObject(wall30); Wall wall49 = new Wall(); addObject(wall49, 7, 1); Wall wall50 = new Wall(); addObject(wall50, 8, 1); removeObject(wall26); Wall wall51 = new Wall(); addObject(wall51, 5, 1); removeObject(wall20); removeObject(wall6); Wall wall52 = new Wall(); addObject(wall52, 1, 4); removeObject(wall52); Wall wall53 = new Wall(); addObject(wall53, 0, 4); Wall wall54 = new Wall(); addObject(wall54, 3, 7); removeObject(wall54); removeObject(wall13); removeObject(wall14); Wall wall55 = new Wall(); addObject(wall55, 3, 8); Wall wall56 = new Wall(); addObject(wall56, 1, 8); Wall wall57 = new Wall(); addObject(wall57, 2, 8); Wall wall58 = new Wall(); addObject(wall58, 3, 9); removeObject(wall55); Wall wall59 = new Wall(); addObject(wall59, 4, 8); removeObject(wall58); removeObject(wall59); removeObject(wall48); Wall wall60 = new Wall(); addObject(wall60, 1, 0); Wall wall61 = new Wall(); addObject(wall61, 3, 9); Wall wall62 = new Wall(); addObject(wall62, 4, 9); removeObject(wall45); Wall wall63 = new Wall(); addObject(wall63, 6, 9); Wall wall64 = new Wall(); addObject(wall64, 9, 8); Wall wall65 = new Wall(); addObject(wall65, 8, 9); removeObject(wall65); } }
davmac davmac

2014/5/2

#
Please use code tags when you post your code, so that we can refer to the code by line number and copy/paste it easily. You're getting a NullPointerException on this line in the Home class:
 int x2 = Greenfoot.getRandomNumber(getWorld().getWidth()-1);
The reason is, you are calling 'getWorld()' before the object has been added into the world; it therefore returns null, and when you then call getWidth() this gives an exception. You need to postpone calling getWorld().getWidth() until after the object has been added to the world. You should do it in the addedToWorld(World) method, which you need to override.
protected void addedToWorld(World world)
{
    x2 = Greenfoot.getRandomNumber(world.getWidth()-1);
    y2 = Greenfoot.getRandomNumber(world.getHeight()-1); 
}
Also, I don't think you need to subtract 1 from the world width and height. For instance, If the width is 600, Greenfoot.getRandomNumber(600) will return a value between 0 and 599 inclusive.
You need to login to post a reply.