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

2011/9/10

Adding an Actor

Canning Canning

2011/9/10

#
I have an Actor called Car. Currently I am adding a Car object via this call: addObject(new Car(), xPos, yPos); However, I am wanting to reference this Car in some code, so I have modified this to have this code at the top: private Car aCar; and am trying to add this Car object via this code: addObject(aCar, xPos, yPos); When I try and compile this, I get the following terminal data: java.lang.NullPointerException at greenfoot.World.addObject(World.java:392) at CarWorld.CreateCar(CarWorld.java:34) at CarWorld.<init>(CarWorld.java:29) 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:520) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:406) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:411) at greenfoot.core.Simulation.maybePause(Simulation.java:269) at greenfoot.core.Simulation.runContent(Simulation.java:201) at greenfoot.core.Simulation.run(Simulation.java:194) What am I doing wrong? thanks
Canning Canning

2011/9/10

#
OK, sorry guys, easily fixed. Changed it to the following: aCar = new Car(); addObject(aCar, xPos, yPos); Now it works.
You need to login to post a reply.