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

2016/5/13

why does greenfoot give null pointer exception before program start?

fandyrazzle fandyrazzle

2016/5/13

#
here is MyWorld code
public class MyWorld extends World
{
    public MyWorld()
    {    
        Zombie zombie=new Zombie();
        zombie.spawn();
	}
}
here is my Zombie code
public class Zombie extends Actor
{
    public void act() 
    {
    }
    public void spawn(){
		getWorld().addObject(new Zombie(), 200, 200);
    }
}
the error was
java.lang.NullPointerException
	at Zombie.spawn(Zombie.java:34)
	at MyWorld.<init>(MyWorld.java:33)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
	at greenfoot.core.Simulation.newInstance(Simulation.java:607)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$4.run(WorldHandlerDelegateIDE.java:445)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:494)
	at greenfoot.core.Simulation.maybePause(Simulation.java:299)
	at greenfoot.core.Simulation.runContent(Simulation.java:212)
	at greenfoot.core.Simulation.run(Simulation.java:205)
why i can't addobject from actor before program start?
SPower SPower

2016/5/13

#
You have not yet added the zombie object you have created to the world. Because of this, getWorld returns null, and that gives you a Null pointer exception. Before calling zombie.spawn(), you'll have to add it:
addObject(zombie, x, y);
Obviously, you'll have to pick values for x and y.
fandyrazzle fandyrazzle

2016/5/13

#
SPower wrote...
You have not yet added the zombie object you have created to the world. Because of this, getWorld returns null, and that gives you a Null pointer exception. Before calling zombie.spawn(), you'll have to add it:
addObject(zombie, x, y);
Obviously, you'll have to pick values for x and y.
Thank you SPower. I summed up, because i have not added
addObject(zombie, x, y);
so when i use
getWorld()
in actor, Greenfot says null. So i changed addObject(zombie, x, y); into
MyWorld world=new MyWorld();
        world.addObject(this, 200, 200);
now the error is
java.lang.StackOverflowError
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.server.MarshalOutputStream.<init>(MarshalOutputStream.java:66)
	at sun.rmi.server.MarshalOutputStream.<init>(MarshalOutputStream.java:55)
	at sun.rmi.transport.ConnectionOutputStream.<init>(ConnectionOutputStream.java:62)
	at sun.rmi.transport.StreamRemoteCall.getOutputStream(StreamRemoteCall.java:102)
	at sun.rmi.transport.StreamRemoteCall.getOutputStream(StreamRemoteCall.java:93)
	at sun.rmi.transport.StreamRemoteCall.<init>(StreamRemoteCall.java:71)
	at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:145)
	at rmiextension.wrappers.RProjectImpl_Stub.getPackage(Unknown Source)
	at greenfoot.core.GProject.getPackage(GProject.java:131)
	at greenfoot.core.GProject.getDefaultPackage(GProject.java:122)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE.getLastWorldGClass(WorldHandlerDelegateIDE.java:505)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE.objectAddedToWorld(WorldHandlerDelegateIDE.java:616)
	at greenfoot.core.WorldHandler.objectAddedToWorld(WorldHandler.java:1011)
	at greenfoot.World.addObject(World.java:416)
	at MyWorld.<init>(MyWorld.java:31)
	at Zombie.spawn(Zombie.java:33)
SPower SPower

2016/5/13

#
That does not work either, because you are creating a new world that is not set as the current world. What you have currently done is create a new MyWorld instance each time you call spawn, but that MyWorld will also call spawn, which will create a new MyWorld, which will create a loop that will never stop (that is, until the computer runs out of memory, which is signaled by a Stack Overflow Error). All you need to do is remove the spawn method and and change the MyWorld constructor to:
public MyWorld()
{
    Zombie zombie = new Zombie();
    addObject(zombie, 200, 200);
}
I don't quite understand the point behind the spawn method as you have currently implemented it: if you'd ask me, there's no point for it.
danpost danpost

2016/5/13

#
If you want zombies to be created from the Zombie class, you will have to make the following changes: Change the 'spawn' method in the Zombie class to this:
public static void spawn(World world)
{
    Actor zombie = new Zombie();
    world.addObject(zombie, 200, 200);
}
Then in the your MyWorld class, you can create a zombie with this:
Zombie.spawn(this);
You need to login to post a reply.